在同一台计算机上开发和使用相同的Python
我正在开发一个 Python 实用程序模块来帮助进行文件下载、存档等。我在虚拟环境中设置了一个项目以及单元测试。当我想在同一台计算机上使用此模块(本质上作为“生产”)时,我将文件移动到 ~/dev/modules/mymodule< 中的 mymodule 目录/strong>
我将所有第 3 方模块保留在 ~/dev/modules/contrib 下。这个 contrib 路径在我的 PYTHONPATH 上,但 mymodule 不在我的 PYTHONPATH 上,因为我注意到如果 mymodule 在我的 PYTHONPATH 上,我的单元测试无法区分“开发”版本和“生产”版本。但现在如果我想使用这个通用实用程序模块,我必须手动将其添加到 PYTHONPATH 中。
这可行,但我确信有更好、更自动化的方法。
在同一台计算机上拥有开发和生产模块的最佳方法是什么?例如,有没有办法动态设置 PYTHONPATH?
I'm developing a Python utility module to help with file downloads, archives, etc. I have a project set up in a virtual environment along with my unit tests. When I want to use this module on the same computer (essentially as "Production"), I move the files to the mymodule directory in the ~/dev/modules/mymodule
I keep all 3rd-party modules under ~/dev/modules/contrib. This contrib path is on my PYTHONPATH, but mymodule is NOT because I've noticed that if mymodule is on my PYTHONPATH, my unit tests cannot distinguish between the "Development" version and the "Production" version. But now if I want to use this common utility module, I have to manually add it to the PYTHONPATH.
This works, but I'm sure there's a better, more automated way.
What is the best way to have a Development and Production module on the same computer? For instance, is there a way to set PYTHONPATH dynamically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以在
sys.path
处添加/修改python路径,只需确保第一个路径是当前目录"."
,因为有些第三方模块依赖导入从当前模块的目录。有关 python 路径的更多信息:
http://djangotricks.blogspot.com/2008/09/note -on-python-paths.html
You can add/modify python paths at
sys.path
, just make sure that the first path is the current directory"."
, because some third-party modules rely on importing from the directory of the current module.More information on python paths:
http://djangotricks.blogspot.com/2008/09/note-on-python-paths.html
我猜你所说的虚拟环境是指 virtualenv 包?
http://pypi.python.org/pypi/virtualenv
我会尝试什么(以及如果我没有正确理解问题,请原谅)是:
然后,为了测试:
对于生产:
也就是说,我写这篇文章的人没有真正尝试过设置愤怒地使用 virtualenv 并希望我已经模糊地理解了你的问题......;)
I'm guessing by virtual environment you mean the virtualenv package?
http://pypi.python.org/pypi/virtualenv
What I'd try (and apologies if I've not understood the question right) is:
Then, for testing:
And, for production:
That said, I write this with the confidence of someone who's not actually tried setting using virtualenv in anger and the hope I've vaguely understood your question... ;)
您可以将
PYTHONPATH
设置为指向生产代码的全局环境变量,然后在要使用开发代码的任何 shell 中,将PYTHONPATH
更改为指向到该代码。(是不是太简单了?我是不是漏掉了什么?)
You could set the
PYTHONPATH
as a global environment variable pointing to your Production code, and then in any shell in which you want to use the Development code, change thePYTHONPATH
to point to that code.(Is that too simplistic? Have I missed something?)