Virtualenv: How to make a custom Python include shared by multicomponents hosted by uWSGI

时间:2015-07-31 20:34:22

标签: python nginx include virtualenv uwsgi

I have a somewhat intricate project setup consisting of several components that work together. Each component is a separate Python project that is hosted as a uWSGI application behind an Nginx proxy. The components interact with each other and with the outside world through the proxy.

I noticed myself about to cut-and-paste some code from one component to another, as they perform similar functions, but interact with different services. Obviously, I want to avoid this, so I am going to pull out common functionality and put it into a separate 'library' project, to be referenced by the different components.

I am running these apps in a virtual environment (using virtualenv), so it should theoretically be easy to simple drop the library project into .env/includes.

However, I have a bit of a strange setup. First of all, I am running the project from /var/www (i.e. uWSGI hosts the apps from here), but the projects actually are present in another source controlled directory. For various reasons, I don't want to move them, so I created symlinks for the project directories in /var/www. This works fine. However, now I have a potential problem, namely, where do I put the library project (which is currently in the same directory as the other components), which I also want to symlink?

Do I symlink it in .env/includes? And if so, how should I reference the library from my other components? Do I reference it from sys.path or as a sibling directory? Is Nginx/uWSGI with virtualenv following the symlinks and taking into account the actual directory or is it blindly assuming that everything is in /var/www?

I have not tried either approach because there seems to be a massive scope for problems, so I wanted to get some input first. Needless to say, I am more than a little confused.

1 个答案:

答案 0 :(得分:2)

我通过在.env / lib / python2.7 / site-packages中对感兴趣的包进行符号链接,很容易解决了这个问题。我最初尝试对整个项目文件夹进行符号链接,但由于无法找到该包,因此无效。

似乎我的uWSGI / Nginx只是遵循virtualenv的pythonpath版本,所以无论我配置什么,都会使用它。

要记住每个包的符号链接会有点痛苦,但至少我只需要为每个包执行一次。

我正在使用PyDev,它掩盖了这个问题,因为我使用的是默认的Python解释器,而不是virtualenv中的解释器。一旦我改变了,就更容易解决。