简体   繁体   English

Apache/wsgi 使用错误的 python 版本

[英]Apache/wsgi using wrong python version

I have two versions of python installed on ubuntu -- 3.7 and 2.7.我在 ubuntu 上安装了两个版本的 python——3.7 和 2.7。 When I add in a "normal" sites-enabled/000-default.conf config, it is trying to use python2.7, so it doesn't find the installed application:当我添加一个“正常” sites-enabled/000-default.conf配置时,它正在尝试使用 python2.7,所以它找不到已安装的应用程序:

WSGIScriptAlias / home/david/data-py/wsgi.py
<VirtualHost *:80>
    <Directory /home/david/data-py>
       Order allow,deny
       Allow from all
       Require all granted
    </Directory>
</VirtualHost>

ImportError: No module named django.core.wsgi导入错误:没有名为 django.core.wsgi 的模块

However, if I specify the python path of my project (which was created using $ virtualenv. , and I start the file with:但是,如果我指定我的项目的 python 路径(它是使用$ virtualenv.创建的,并且我使用以下文件启动文件:

WSGIScriptAlias / home/david/data-py/wsgi.py
WSGIPythonHome /home/david/data-py

Then I get another error:然后我得到另一个错误:

ImportError: No module named site导入错误:没有名为站点的模块

What does this mean and how can I get the project installed in apache/wsgi?这是什么意思,我怎样才能在 apache/wsgi 中安装项目?

I just ran into the exact same issue and it turns out I had installed the wrong version of mod_wsgi on my server (I had the one for python 2 not python 3). 我刚刚遇到了完全相同的问题,事实证明我在我的服务器上安装了错误版本的mod_wsgi(我有一个用于python 2而不是python 3)。 You can see what version apache is attempting to use if you look at the tracebacks stored in the apache error.log file (in my case /var/log/apache2/error.log) I solved it as follows (on Ubuntu): 你可以看看apache尝试使用的版本,如果你看一下存储在apache error.log文件中的回溯(在我的案例中是/var/log/apache2/error.log)我解决了它如下(在Ubuntu上):

sudo apt-get remove libapache2-mod-python libapache2-mod-wsgi 
sudo apt-get install libapache2-mod-wsgi-py3
sudo service apache2 restart

Also I made sure to install the mod_wsgi package into my virtual environment (I'm not sure whether this was necessary). 另外我确保将mod_wsgi包安装到我的虚拟环境中(我不确定这是否必要)。 To do this I did the following: 为此,我做了以下事情:

cd <path to my virtual env bin folder>
sudo ./pip3 install mod_wsgi-httpd 
sudo ./pip3 install mod-wsgi

I also ran into the same issue about a month ago (caused by python2 vs python3). 我也在一个月前遇到了同样的问题(由python2和python3引起)。 Here is what I recommend: 这是我的建议:

  1. Remove django and pip from python2 (this will avoid the confusion) 从python2中删除django和pip(这样可以避免混淆)
  2. Python3 comes with inbuilt pip3 - so go ahead and install/update django Python3附带内置pip3 - 所以继续安装/更新django
  3. Remove the mod_wsgi.so you have installed/setup at OS level 删除已在OS级别安装/设置的mod_wsgi.so
  4. Install the python3-dev/python3-devel+apache-dev/apache-devel package 安装python3-dev / python3-devel + apache-dev / apache-devel包
  5. Try pip3 install mod-wsgi - if it fails use the steps mentioned here ( https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html ) 尝试pip3 install mod-wsgi - 如果失败则使用此处提到的步骤( https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html
  6. More troubleshooting at https://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html 有关详细信息, 访问https://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html

As for me, I've had to process these steps to solve the error:至于我,我不得不处理这些步骤来解决错误:

  1. Go to the pyvenv.cfg file in the Virtual environment folder Go到Virtual environment文件夹下的pyvenv.cfg文件
  2. Set the include-system-site-packages to true and save the change将 include-system-site-packages 设置为 true 并保存更改
  3. Reactivate the virtual environment.重新激活虚拟环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM