Python 如何使用 virtualenv 运行 uwsgi
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18417823/
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
How do I run uwsgi with virtualenv
提问by Asken
I'm currently developing my first real python flask project and am about to set up the build server to deploy the "Latest Build" which is built on every check-in.
我目前正在开发我的第一个真正的 python Flask 项目,并且即将设置构建服务器以部署在每次签入时构建的“最新构建”。
I have set up a startup script where I start the application using uwsgi and this part is working fine. I have recently also started using virtualenv
and by doing so the packages installed are added to my project under projectname\flask\Lib\site-packages
.
我已经设置了一个启动脚本,我使用 uwsgi 启动应用程序,这部分工作正常。我最近也开始使用virtualenv
,通过这样做,安装的包被添加到我的项目中projectname\flask\Lib\site-packages
。
I'm using nginx
as the web server and the config looks like this:
我nginx
用作 Web 服务器,配置如下所示:
location / { try_files $uri @graderbuild; }
location @graderbuild {
include uwsgi_params;
uwsgi_param UWSGI_CHDIR /usr/local/grader/build;
uwsgi_param UWSGI_PYHOME /usr/local/grader/build;
uwsgi_pass 127.0.0.1:3031;
}
I'm starting uwsgi
using this:
我开始uwsgi
使用这个:
exec /usr/local/bin/uwsgi --master --socket 127.0.0.1:3031
--wsgi-file restserver.py --callable app --processes 4 --die-on-term
--threads 2 >> /var/log/grader-build.log 2>&1
Now to where I know if I'm doing it right... currently I am deploying the entire folder to the build server. I don't want to install global python modules just to get my build to work. Right or wrong?
现在我知道我是否做对了......目前我正在将整个文件夹部署到构建服务器。我不想安装全局 python 模块只是为了让我的构建工作。对还是错?
The error I get currently is:
我目前得到的错误是:
ImportError: No module named flask_wtf
If I'm right, how do I configure the setup to use the virtualenv
site-packages? My preferred location would be in the startup
script and not in the nginx
config.
如果我是对的,我该如何配置设置以使用virtualenv
站点包?我的首选位置是在startup
脚本中而不是在nginx
配置中。
采纳答案by iMom0
Use -H
to set virtualenv to python path.
使用-H
来设定的virtualenv到Python路径。
uwsgi -H /path/to/your/virtualenv
http://uwsgi-docs.readthedocs.org/en/latest/Options.html#virtualenv
http://uwsgi-docs.readthedocs.org/en/latest/Options.html#virtualenv
回答by Palasaty
As user995394 pointedout, there is a way to tell uWSGI use existing virtual environment.
However, when I pass uWSGI option in form virtualenv = /full/path/to/my/virtualenv
(it's from INI config) it complains about ImportError: No module named site
. The workaround I found is that you launch uWSGI from folder where your virtualenv is and pass just virtualenv = my_virtualenv_name
(i.e. path is relative).
正如 user995394 所指出的,有一种方法可以告诉 uWSGI 使用现有的虚拟环境。但是,当我以表单virtualenv = /full/path/to/my/virtualenv
(它来自 INI 配置)传递 uWSGI 选项时,它会抱怨ImportError: No module named site
. 我发现的解决方法是从 virtualenv 所在的文件夹启动 uWSGI 并只传递virtualenv = my_virtualenv_name
(即路径是相对的)。
I use uWSGI 2.0.
我使用 uWSGI 2.0。
回答by Beau
To use the activated virtualenv you can use this config snippet in your uwsgi.ini
:
要使用激活的 virtualenv,您可以在您的uwsgi.ini
:
; If VIRTUAL_ENV is set then use its value to specify the virtualenv directory
if-env = VIRTUAL_ENV
virtualenv = %(_)
endif =
回答by tourdownunder
I had this issue a few months back and have a full example of demo configs here including nginx, uwsgi starting automatically with upstart on linux.
几个月前我遇到了这个问题,这里有一个完整的演示配置示例,包括 nginx,uwsgi 在 linux 上自动启动新贵。
回答by user4830534
Beau's answer resolved this issue for me.
博的回答为我解决了这个问题。
I never did find a good explanation for uwsgi's ini file directives.
我从来没有找到对 uwsgi 的 ini 文件指令的好的解释。
Until Beau's answer I never saw an answer to what explicitly
the virtualenv value should be set to - the root of the python tree in the venv, the app's folder under site-packages or the root of the VENV TREE. What if you aren't using a venv, what do you set home to, top of app tree, top of python bin folder, python lib folder or to dist-packages?
在 Beau 的回答之前,我从来没有看到explicitly
应该将 virtualenv 值设置为什么的答案- venv 中 python 树的根、站点包下的应用程序文件夹或 VENV TREE 的根。如果您不使用 venv,您将主目录设置为应用程序树的顶部、python bin 文件夹的顶部、python lib 文件夹还是 dist-packages?
I have this app working on another system, so it really shouldn't have been that difficult to run it under a docker container. Now that I have got it working I reviewed that working installation and now see it points to the top of the venv tree. I was using virtualenvwrapper there, so it's a different path than when using only virtualenv.
我有这个应用程序在另一个系统上运行,所以在 docker 容器下运行它真的不应该那么困难。现在我已经让它工作了,我查看了工作安装,现在看到它指向 venv 树的顶部。我在那里使用 virtualenvwrapper,所以它与仅使用 virtualenv 时的路径不同。
It makes me wonder if it's possible to run this app without the venv. Since this will run in a docker container there isn't really a good reason to use venvs, but in looking at the python folder structure differences they are quite different between system python and venv python.
这让我想知道是否可以在没有 venv 的情况下运行这个应用程序。由于这将在 docker 容器中运行,因此使用 venvs 并不是一个很好的理由,但是在查看 python 文件夹结构差异时,它们在系统 python 和 venv python 之间有很大不同。
System's python3 is split into separate folders and the files are not all under a single hierarchy as they are under a venv. If you install your packages with pip they will end up in /usr/local/lib/python3/dist-packages, and that location does NOT have site.py or encodings folders which is why so many have import errors.
系统的 python3 被拆分为单独的文件夹,并且文件并不都在一个单一的层次结构下,因为它们在一个 venv 下。如果你用 pip 安装你的包,它们最终会在 /usr/local/lib/python3/dist-packages 中,并且该位置没有 site.py 或 encodings 文件夹,这就是为什么有这么多导入错误的原因。
After only a few trials I discovered that to run my app without a venv the uwsgi ini should NOT define either home OR virtualenv settings. If your system path includes both /usr/bin AND /usr/local/bin it should work and find everything, even tho pip installed packages go somewhere else with a different folder hierarchy.
经过几次试验,我发现要在没有 venv 的情况下运行我的应用程序,uwsgi ini 不应定义 home 或 virtualenv 设置。如果您的系统路径同时包含 /usr/bin 和 /usr/local/bin ,它应该可以工作并找到所有内容,即使 pip 安装的软件包会转到具有不同文件夹层次结构的其他地方。
回答by Yaroslav Nikitenko
Others' answers didn't help, and I added path to virtualenv to uwsgi.ini configuration file. The error disappeared.
其他人的回答没有帮助,我将 virtualenv 的路径添加到 uwsgi.ini 配置文件。错误消失了。
pythonpath = /path-to-virtualenv/project/lib/python2.7/site-packages