Python uwsgi + Flask + virtualenv 导入错误:没有名为站点的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17391605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
uwsgi + Flask + virtualenv ImportError: no module named site
提问by 010110110101
(Other posts on SO are similar, but none have the specific combination of uwsgi + Flask + virtualenv) (This one is closest)
(SO 上的其他帖子类似,但都没有 uwsgi + Flask + virtualenv 的特定组合)(这是最接近的)
I installed uwsgi via apt-get. I also tried pip install wsgi. Both gave me the same issue.
我通过 apt-get 安装了 uwsgi。我也试过 pip install wsgi。两者都给了我同样的问题。
Test command:
测试命令:
sudo uwsgi -s /tmp/uwsgi.sock -w myapp:app -H myvirtualenv
Result:
结果:
Python version: 2.7.4 (default, Apr 19, 2013, 18:35:44) [GCC 4.7.3]
Set PythonHome to myvirtualenv
ImportError: No module named site
I can otherwise run my app in the virtual env.
否则我可以在虚拟环境中运行我的应用程序。
采纳答案by hughes
See the answer from @JRajan first.
首先查看@JRajan 的回答。
If you're sure you just want to suppressthe error and not actually solvethe underlying issue, you should add --no-site
to your command or no-site=true
to your uwsgi.ini file.
如果您确定只想抑制错误而不实际解决潜在问题,您应该添加--no-site
到您的命令或no-site=true
您的 uwsgi.ini 文件中。
回答by JRajan
The path to your virtual environment is wrong. That's the reaon for this error.
虚拟环境的路径错误。这就是这个错误的原因。
I'm using virtualenvwrapper and my virtual environments are set at ~/.virtualenvs. So in my case, the uwsgi call would look something like
我正在使用 virtualenvwrapper,我的虚拟环境设置在 ~/.virtualenvs。所以在我的情况下,uwsgi 调用看起来像
sudo uwsgi -s /tmp/uwsgi.sock -w myapp:app -H ~/.virtualenvs/myapp
Hope this helps next time someone comes looking for this one.
希望下次有人来找这个时这会有所帮助。
Thanks to Cody for pointing it out in the comments.
感谢 Cody 在评论中指出。
回答by shanemgrey
THIS MAY BE BAD FOR SECURITY FOR SEVERAL REASONS. IT WORKS FOR TESTING. BUT REVIEW FOR SECURITY BEFORE USING THIS EXACT SOLUTION IN PRODUCTION
出于多种原因,这可能不利于安全。它适用于测试。但在生产中使用此精确解决方案之前请检查安全性
Another reason this error may occur is permissions related. If using an .ini file as described in the official tutorial for uWSGI for django, you may have created the ini file with a user and group that makes the file inaccessible to the user that's running the process.
可能发生此错误的另一个原因是权限相关。如果使用 .ini 文件,如 uWSGI for django 的官方教程中所述,您可能已经使用用户和组创建了 ini 文件,这使得运行该进程的用户无法访问该文件。
Check the owner and permissions for the file and the directory path it's in. Use chown and chmod to set the needed permissions.
检查文件的所有者和权限以及它所在的目录路径。使用 chown 和 chmod 设置所需的权限。
sudo chown -R www-data:www-data /srv
sudo chown -R www-data:www-data /srv
sudo chmod 0775 -R /srv
sudo chmod 0775 -R /srv
In my case, I was using a vagrant box for testing and the default user is "vagrant" while nginx is using www-data for the user and group. I have set the owner of all of the files in the project to the www-data user and group and added the vagrant user to the www-data group.
就我而言,我使用 vagrant box 进行测试,默认用户是“vagrant”,而 nginx 为用户和组使用 www-data。我已将项目中所有文件的所有者设置为 www-data 用户和组,并将 vagrant 用户添加到 www-data 组。
sudo gpasswd -a vagrant www-data
sudo gpasswd -a vagrant www-data
I'm not sure if this is good security practice, so I'll be working with my system admin when the time comes to put it in production. But for my test environment it works. Either way, permissions will be something to look closely at for many of these issues.
我不确定这是否是良好的安全实践,因此当需要将其投入生产时,我将与我的系统管理员合作。但是对于我的测试环境它有效。无论哪种方式,对于许多这些问题,权限都是需要仔细研究的。
回答by luislhl
In my case the problem was the python version uWSGI tried to use.
就我而言,问题是 uWSGI 尝试使用的 python 版本。
My project was written in python 3.4, but I was not specifying this in uWSGI config. So uWSGI tried to use python 2 and tried to import modules from the folder lib/python2.7 inside the virtualenv.
我的项目是用 python 3.4 编写的,但我没有在 uWSGI 配置中指定它。所以uWSGI尝试使用python 2并尝试从virtualenv中的文件夹lib/python2.7导入模块。
So I received the 'No module named site' error, because all the modules, including the site module, where inside lib/python3.4, not lib/python2.7.
所以我收到了“没有名为站点的模块”错误,因为所有模块,包括站点模块,都在 lib/python3.4 内部,而不是 lib/python2.7。
To solve it, i had to do two things:
为了解决这个问题,我必须做两件事:
Install the python3 plugin for uWSGI, with:
apt-get install uwsgi-plugin-python3
Use it in the .ini config file, with:
plugins = python34
为 uWSGI 安装 python3 插件,使用:
apt-get install uwsgi-plugin-python3
在 .ini 配置文件中使用它,包括:
plugins = python34
Hope this helps someone with the same problem in the future.
希望这可以帮助将来遇到同样问题的人。
As requested, here follows my .ini file:
根据要求,下面是我的 .ini 文件:
[uwsgi]
base = /your/app/path
pythonpath = %(base)
module = your_module_name
callable = app #Here you put the name of the variable which holds your app inside your module
home = /your/virtualenv/path
plugins = python34
master = true
processes = 2
uid = www-data
gid = www-data
socket = /path/to/socket
chmod-socket = 660
die-on-term = true
logto = /var/log/uwsgi/%n.log
回答by kofwang
I had the similar issue before. My problem is that I have both python2.x and python3.x on my ubuntu system, and I want my project to run in a virtual env in which python3 environment is installed. How I resolved this issue:
我以前有过类似的问题。我的问题是我的 ubuntu 系统上有 python2.x 和 python3.x,我希望我的项目在安装了 python3 环境的虚拟环境中运行。我是如何解决这个问题的:
apt-get install python3-pip
pip3 install uWSGI
apt-get 安装 python3-pip
pip3 安装 uWSGI
That's all.
就这样。
回答by Faith Nassiwa
I encountered the same and my problem was with the python version where uwsgi was running. uwsgi was running on python2 yet my virtualenv python path was set to python3. This created the conflict, it kept on failing to locate the installed site package.
我遇到了同样的问题,我的问题是运行 uwsgi 的 python 版本。uwsgi 在 python2 上运行,但我的 virtualenv python 路径设置为 python3。这造成了冲突,它一直无法找到已安装的站点包。
Double check the python version where uwsgi is running so that it is the same as that set on your virtualenv.
仔细检查运行 uwsgi 的 python 版本,使其与您在 virtualenv 上设置的版本相同。
回答by Abu Obaida
If your virtual environment is running on Python3 then you must install uwsgi using pip3, not pip otherwise, version mismatch will create this import problem
如果您的虚拟环境在 Python3 上运行,那么您必须使用 pip3 安装 uwsgi,而不是 pip 否则,版本不匹配将导致此导入问题
pip uninstall uwsgi
pip3 install uwsgi
回答by Nikita Alexander
I had this problem when my homebrew updated my Python version to Python 3.7 and it stopped working. What worked for me was brew info python
- that will show you all your available Python versions. Than I rolled back to Python 3.6.5 using brew switch python 3.6.5
.
当我的自制软件将我的 Python 版本更新为 Python 3.7 并且它停止工作时,我遇到了这个问题。对我有用的是brew info python
- 这将向您显示所有可用的 Python 版本。比我回滚到 Python 3.6.5 使用brew switch python 3.6.5
.
After that I simply reinstalled my uWSGI using:
之后,我只是使用以下命令重新安装了我的 uWSGI:
pip3 uninstall uwsgi
pip3 install uwsgi
And that solved it. If you're not sure what version of Python you were using, brew info python
shows you the install dates. Also, you can check using pip3 list
if your uWSGI is installed for the current version.
这解决了它。如果您不确定所使用的 Python 版本,请brew info python
显示安装日期。此外,您可以检查pip3 list
是否为当前版本安装了 uWSGI。
Hope this helps!
希望这可以帮助!