简体   繁体   English

Virtualenv:如何使自定义Python包含由uWSGI托管的多组件共享

[英]Virtualenv: How to make a custom Python include shared by multicomponents hosted by 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. 每个组件都是一个单独的Python项目,作为Nginx代理后面的uWSGI应用程序托管。 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. 我在虚拟环境中运行这些应用程序(使用virtualenv),因此从理论上讲,将库项目简单地放入.env / includes中应该很容易。

However, I have a bit of a strange setup. 但是,我有一个奇怪的设置。 First of all, I am running the project from /var/www (ie uWSGI hosts the apps from here), but the projects actually are present in another source controlled directory. 首先,我从/ var / www运行项目(即uWSGI从这里托管应用程序),但项目实际上存在于另一个源控制目录中。 For various reasons, I don't want to move them, so I created symlinks for the project directories in /var/www. 由于各种原因,我不想移动它们,因此我在/ 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? 我可以在.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? 我是从sys.path引用它还是作为兄弟目录引用它? 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? Nginx / uWSGI是否遵循符号链接并使用virtualenv并考虑到实际目录,还是盲目地假设一切都在/ 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. 不用说,我有点困惑。

I solved the problem quite easily by symlinking the package of interest in .env/lib/python2.7/site-packages. 通过在.env / lib / python2.7 / site-packages中对感兴趣的包进行符号链接,我很容易解决了这个问题。 I originally tried to symlink the entire project folder but that didn't work as it couldn't find the package. 我最初尝试对整个项目文件夹进行符号链接,但由于无法找到该包,因此无效。

It seems that my uWSGI/Nginx just follows the virtualenv's version of pythonpath, so whatever I configure there is used. 看来我的uWSGI / Nginx只是遵循virtualenv的pythonpath版本,所以无论我配置什么,都会使用它。

It will be a bit of a pain to have to remember to symlink every package, but at least I only have to do it once for each package. 记住每个包的符号链接会有点痛苦,但至少我只需要为每个包执行一次。

I'm using PyDev, and it was masking the issue because I was using the default Python interpreter, not the one in virtualenv. 我正在使用PyDev,它掩盖了这个问题因为我使用的是默认的Python解释器,而不是virtualenv中的解释器。 Once I changed that, it was easier to solve. 一旦我改变了,就更容易解决。

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

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