docker-python 错误:ModuleNotFoundError:没有名为“_distutils_hack”的模块
我正在使用 docker 和 apache2 运行一个 python/django 应用程序,它运行得很好,但是当我尝试再次运行时突然出现此错误:Traceback(最近一次调用最后一次):文件 \'...
我正在使用 docker 和 apache2 运行一个 python/django 应用程序,它运行良好,但是当我尝试再次运行时突然出现此错误:
Traceback (most recent call last):
File "<frozen site>", line 201, in addpackage
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named '_distutils_hack'
Remainder of file ignored
Traceback (most recent call last):
File "<frozen site>", line 201, in addpackage
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named '_distutils_hack'
Remainder of file ignored
Error processing line 1 of /usr/lib/python3/dist-packages/distutils-precedence.pth:
Traceback (most recent call last):
File "<frozen site>", line 201, in addpackage
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named '_distutils_hack'
Remainder of file ignored
[Wed May 29 15:43:45.395312 2024] [wsgi:error] [pid 26:tid 132520276391616] [client 172.27.0.1:40422] mod_wsgi (pid=26): Failed to exec Python script file '/var/www/html/demo_app/water_maps/wsgi.py'.
[Wed May 29 15:43:45.395343 2024] [wsgi:error] [pid 26:tid 132520276391616] [client 172.27.0.1:40422] mod_wsgi (pid=26): Exception occurred processing WSGI script '/var/www/html/demo_app/water_maps/wsgi.py'.
[Wed May 29 15:43:45.395501 2024] [wsgi:error] [pid 26:tid 132520276391616] [client 172.27.0.1:40422] Traceback (most recent call last):
[Wed May 29 15:43:45.401671 2024] [wsgi:error] [pid 26:tid 132520276391616] [client 172.27.0.1:40422] File "/var/www/html/demo_app/water_maps/wsgi.py", line 12, in <module>
[Wed May 29 15:43:45.401687 2024] [wsgi:error] [pid 26:tid 132520276391616] [client 172.27.0.1:40422] from django.core.wsgi import get_wsgi_application
[Wed May 29 15:43:45.401704 2024] [wsgi:error] [pid 26:tid 132520276391616] [client 172.27.0.1:40422] ModuleNotFoundError: No module named 'django'
以下是项目中使用的文件的示例:
Dockerfile
FROM ubuntu
RUN apt-get update
# Avoid tzdata infinite waiting bug
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Africa/Cairo
RUN apt clean
RUN apt-get update
RUN apt-get install -y apt-utils vim curl apache2 apache2-utils git
RUN apt -y install software-properties-common
RUN apt update
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt -y install python3.10-full
# Optional: Set Python 3.9 as the default Python version
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
# RUN apt-get -y install python3 libapache2-mod-wsgi-py3
RUN apt-get -y install libapache2-mod-wsgi-py3
RUN apt -y install certbot python3-certbot-apache
RUN apt-get -y install python3-pip
RUN apt update
# Check Python and pip versions
RUN python3 --version && \
pip3 --version
#Add sf to avoid ln: failed to create hard link '/usr/bin/pip': File exists
RUN ln -sf /usr/bin/pip3 /usr/bin/pip
RUN pip install --upgrade pip --break-system-packages
RUN pip install django ptvsd --break-system-packages
RUN apt install wait-for-it
RUN pip install cffi --upgrade --break-system-packages
RUN pip install -U pip setuptools --break-system-packages
RUN apt-get -y install gettext
RUN apt-get -y install poppler-utils
RUN apt-get -y install redis-server
RUN apt-get install python3-pymysql
RUN a2enmod headers
RUN service apache2 restart
COPY www/demo_app/water_maps/requirements.txt requirements.txt
RUN python3 -m pip install --upgrade setuptools
RUN pip install -r requirements.txt --break-system-packages
ADD ./demo_site.conf /etc/apache2/sites-available/000-default.conf
EXPOSE 80 5432
WORKDIR /var/www/html/demo_app
#CMD ["apache2ctl", "-D", "FOREGROUND"]
#CMD ["python", "manage.py", "migrate", "--no-input"]
docker-compose.yaml
version: "2"
services:
db:
image: postgres:14
restart: always
volumes:
- ./data/db:/var/lib/postgresql/data
- ./www/:/var/www/html
- ./www/demo_app/kml_files:/var/www/html/demo_app/kml_files
- ./www/demo_app/temp_kml_file:/var/www/html/demo_app/temp_kml_file
- ./www/demo_app/upload:/var/www/html/demo_app/upload
- ./data/log:/var/log/apache2
ports:
- '5432:5432'
environment:
- POSTGRES_DB=database_innvoentiq
- POSTGRES_USER=database_user_innvoentiq
- POSTGRES_PASSWORD=Yahoo000@
django-apache2:
build: .
container_name: water_maps
restart: always
environment:
- POSTGRES_DB=database_innvoentiq
- POSTGRES_USER=database_user_innvoentiq
- POSTGRES_PASSWORD=Yahoo000@
ports:
- 5000:80
- 5001:443
# - 80:80
# - 443:443
volumes:
- ./www/:/var/www/html
- ./www/demo_app/kml_files:/var/www/html/demo_app/kml_files
- ./www/demo_app/temp_kml_file:/var/www/html/demo_app/temp_kml_file
- ./www/demo_app/upload:/var/www/html/demo_app/upload
- ./data/log:/var/log/apache2
# - ./data/config/etc/apache2:/etc/apache2
# command: sh -c 'python3 manage.py migrate && python3 manage.py loaddata the_db.json '
command: sh -c 'wait-for-it db:5432 -- python3 manage.py makemigrations && python3 manage.py migrate && python3 manage.py collectstatic --noinput && python3 manage.py compilemessages && apache2ctl -D FOREGROUND'
# command: sh -c 'wait-for-it db:5432 -- python manage.py migrate && python manage.py loaddata last.json && apache2ctl -D FOREGROUND'
depends_on:
- db
/demo_site.conf
WSGIPythonPath /var/www/html/demo_app
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
# ServerName test3.watermaps-eg.com
# ServerAlias test3.watermaps-eg.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/
Alias /static /var/www/html/demo_app/static
Alias /en/upload /var/www/html/demo_app/upload
Alias /ar/upload /var/www/html/demo_app/upload
Alias /upload /var/www/html/demo_app/upload
WSGIScriptAlias / /var/www/html/demo_app/water_maps/wsgi.py
WSGIPassAuthorization On
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
<Directory /var/www/html/demo_app>
Require all granted
</Directory>
<Directory /var/www/html/demo_app/static>
Require all granted
</Directory>
<Directory /var/www/html/demo_app/upload>
Require all granted
</Directory>
<Directory /var/www/html/demo_app/water_maps>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Header add Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Headers "*"
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
TimeOut 7200
</VirtualHost>
-
我正在尝试使用 Ubuntu 最新映像设置新的 Docker 容器,并使用 PyEnv 安装多个 Python 版本。虽然我成功安装了 Python 3.8.10,但遇到了问题...
我正在尝试使用 Ubuntu 最新映像设置新的 Docker 容器,并使用 PyEnv 安装多个 Python 版本。虽然我成功安装了 Python 3.8.10,但在尝试安装 Python 3.6.8 时遇到了问题。
重现步骤:
-
启动 Docker 容器:
docker run -it --label 'python-envs' -v ~/Desktop/python-envs/:/workspace/python-envs:rw --hostname python --name python-envs ubuntu:latest
系统信息
uname -a
:Linux python 6.5.0-35-generic #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue May 7 09:00:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
-
已安装的 pyenv:
curl https://pyenv.run | bash
验证已安装的 Python 版本,我
3.8.10
之前安装过版本并成功:pyenv versions
输出:
* system (set by /root/.pyenv/version)
3.8.10
- 安装所需的库:
apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
- 尝试安装 Python 3.6.8:
pyenv install 3.6.8
遇到错误:
Downloading Python-3.6.8.tar.xz... -> https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz Installing Python-3.6.8... /root/.pyenv/plugins/python-build/bin/python-build: line 1863: 30193 Segmentation fault (core dumped) "$PYTHON_BIN" -c "import $1" WARNING: The Python ctypes extension was not compiled. Missing the libffi lib? /root/.pyenv/plugins/python-build/bin/python-build: line 2089: 30200 Segmentation fault (core dumped) "$PYTHON_BIN" $python_opts -m ensurepip ${ensurepip_opts} > /dev/null 2>&1 Installing pip from https://bootstrap.pypa.io/pip/3.6/get-pip.py... /root/.pyenv/plugins/python-build/bin/python-build: line 2065: 30204 Segmentation fault (core dumped) "${PYTHON_BIN}" -s "${get_pip}" ${GET_PIP_OPTS} 1>&4 2>&1 error: failed to install pip via get-pip.py BUILD FAILED (Ubuntu 24.04 using python-build 20180424) Inspect or clean up the working tree at /tmp/python-build.20240612063012.18758 Results logged to /tmp/python-build.20240612063012.18758.log Last 10 log lines: install|*) ensurepip="" ;; \ esac; \ LD_LIBRARY_PATH=/tmp/python-build.20240612063012.18758/Python-3.6.8 ./python -E -m ensurepip \ $ensurepip --root=/ ; \ fi Looking in links: /tmp/tmpm_fdsrq7 Collecting setuptools Collecting pip Installing collected packages: setuptools, pip Successfully installed pip-18.1 setuptools-40.6.2
清除以前的尝试:
pyenv uninstall 3.6.8
使用环境变量来调整构建配置:
CFLAGS="-O2 -D_FORTIFY_SOURCE=2 -fstack-protector-strong -fno-plt \ -Wformat -Werror=format-security -fPIC" \ LDFLAGS="-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed" \
这些步骤都无法解决问题。使用 PyEnv 安装 Python 3.6.8 时,什么原因导致分段错误?我需要考虑其他步骤或配置吗?
任何见解或建议都将不胜感激!
-
启动 Docker 容器: