'why does python3.6 container uses the /usr/lib of python 3.9

I start a docker contain with

FROM python:3.6
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y apt-utils vim curl apache2 apache2-utils
RUN apt-get -y install libapache2-mod-wsgi-py3
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools==57.5.0
RUN pip install django==4.0 celery==5.2 ptvsd suds-py3 psycopg2 mysqlclient pysnmp pyparsing python-dateutil spyne lxml supervisor flower
ADD ./site-config.conf /etc/apache2/sites-available/000-default.conf
ADD ./www/project /var/www/html
WORKDIR /var/www/html 
RUN chmod 775 /var/www/html/project
CMD ["apache2ctl", "-D", "FOREGROUND"]

And the apache file:

WSGIPythonPath /var/www/html/project:/usr/local/lib/python3.6/site-packages/

<VirtualHost *:80>
    ServerName localhost
    ServerAdmin webmaster@localhost 
    DocumentRoot /var/www/html/project 
    Alias /static "/var/www/html/project/static"
    WSGIScriptAlias / /var/www/html/project/project/wsgi.py 
    ErrorLog /var/www/html/logs/error.log
    CustomLog /var/www/html/logs/access.log combined 
</VirtualHost>

It installs both python 3.6 and 3.9 as I noticed:

All pips I installed are in 3.6 including mysqlclient

But when it tries to import _mysql, it checks the /usr/lib of python3.9 which gives an error because it in 3.6 not 3.9. File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module ImportError: cannot import name '_mysql' from partially initialized module 'MySQLdb' (most likely due to a circular import) (/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py) Why doe this issue happen and how to prevent it?

Here are the logs for the django application, I get the error:

[Thu Mar 10 11:19:44.400586 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972] mod_wsgi (pid=12660): Exception occurred processing WSGI script '/var/www/html/project/project/wsgi.py'., referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423410 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972] Traceback (most recent call last):, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423482 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 18, in <module>, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423498 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     from . import _mysql, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423533 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972] ImportError: cannot import name '_mysql' from partially initialized module 'MySQLdb' (most likely due to a circular import) (/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423558 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972] , referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423568 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972] During handling of the above exception, another exception occurred:, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423576 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972] , referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423594 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972] Traceback (most recent call last):, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423755 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/var/www/html/project/project/wsgi.py", line 16, in <module>, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423769 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     application = get_wsgi_application(), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423791 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423802 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     django.setup(set_prefix=False), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423821 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423831 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     apps.populate(settings.INSTALLED_APPS), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423849 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 114, in populate, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423870 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     app_config.import_models(), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423889 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/apps/config.py", line 301, in import_models, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423899 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     self.models_module = import_module(models_module_name), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423915 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423924 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     return _bootstrap._gcd_import(name[level:], package, level), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423939 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423958 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423976 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.423995 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424013 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "<frozen importlib._bootstrap_external>", line 790, in exec_module, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424033 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424051 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/models.py", line 3, in <module>, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424062 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424080 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 48, in <module>, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424090 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     class AbstractBaseUser(models.Model):, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424108 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 122, in __new__, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424118 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     new_class.add_to_class('_meta', Options(meta, app_label)), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424143 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 326, in add_to_class, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424154 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     value.contribute_to_class(cls, name), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424172 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/db/models/options.py", line 207, in contribute_to_class, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424184 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424202 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/utils/connection.py", line 15, in __getattr__, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424212 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     return getattr(self._connections[self._alias], item), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424228 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/utils/connection.py", line 62, in __getitem__, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424238 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     conn = self.create_connection(alias), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424254 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 204, in create_connection, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424264 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     backend = load_backend(db['ENGINE']), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424280 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 111, in load_backend, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424290 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     return import_module('%s.base' % backend_name), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424306 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424316 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     return _bootstrap._gcd_import(name[level:], package, level), referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424332 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 15, in <module>, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424342 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     import MySQLdb as Database, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424357 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]   File "/usr/local/lib/python3.6/site-packages/MySQLdb/__init__.py", line 24, in <module>, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424366 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972]     version_info, _mysql.version_info, _mysql.__file__, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.424418 2022] [wsgi:error] [pid 12660:tid 139881606039296] [client 172.23.0.1:43972] NameError: name '_mysql' is not defined, referer: http://localhost:8005/request/outgoing/
[Thu Mar 10 11:19:44.495983 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984] mod_wsgi (pid=12660): Failed to exec Python script file '/var/www/html/project/project/wsgi.py'., referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.496864 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984] mod_wsgi (pid=12660): Exception occurred processing WSGI script '/var/www/html/project/project/wsgi.py'., referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497096 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984] Traceback (most recent call last):, referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497253 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984]   File "/var/www/html/project/project/wsgi.py", line 16, in <module>, referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497269 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984]     application = get_wsgi_application(), referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497292 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984]   File "/usr/local/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application, referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497303 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984]     django.setup(set_prefix=False), referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497321 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984]   File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 24, in setup, referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497331 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984]     apps.populate(settings.INSTALLED_APPS), referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497349 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984]   File "/usr/local/lib/python3.6/site-packages/django/apps/registry.py", line 83, in populate, referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497359 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984]     raise RuntimeError("populate() isn't reentrant"), referer: http://localhost:8005/request/search/
[Thu Mar 10 11:19:44.497391 2022] [wsgi:error] [pid 12660:tid 139881472829184] [client 172.23.0.1:43984] RuntimeError: populate() isn't reentrant, referer: http://localhost:8005/request/search/```


Solution 1:[1]

I had this issue before, I added virtual env with packages I need and added

WSGIDaemonProcess example.com python-home=/path/to/venv python-path=/path/to/mysite.com
WSGIProcessGroup example.com

Check details here Django documents

This answer is not why but a fix + using Venv in your situation is better

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 octopus