在同一台计算机上开发和使用相同的Python

发布于 2024-09-27 10:23:16 字数 573 浏览 8 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花伊自在美 2024-10-04 10:23:16

你可以在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

这个俗人 2024-10-04 10:23:16

我猜你所说的虚拟环境是指 virtualenv 包?

http://pypi.python.org/pypi/virtualenv

我会尝试什么(以及如果我没有正确理解问题,请原谅)是:

  • 将源代码保留在 PYTHONPATH 未引用的位置(例如 ~/projects/myproject)
  • 编写一个简单的 setuptools 或 distutils 脚本来安装它(请参阅 Python distutils - 有人知道如何使用它吗?
  • 使用 virtualenv 包使用 --no-site-packages 选项创建开发虚拟环境 - 这样您的“dev”版本将不会看到默认 python 安装中安装的任何软件包。
  • (还要确保您的 PYTHONPATH 没有任何源目录)

然后,为了测试:

  • 激活开发虚拟环境
  • 运行安装脚本(通常类似于 python setup.py build install)。您的包最终位于 /path/to/dev_virtualenv/lib/python2.x/site-packages/
  • 测试、中断、修复、重复

对于生产:

  • 确保 dev virtualenv 未激活
  • 运行安装脚本
  • 一切顺利, “dev”版本隐藏在生产环境看不到的虚拟环境中...
  • ...并且没有(直接)搞乱 PYTHONPATH

也就是说,我写这篇文章的人没有真正尝试过设置愤怒地使用 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:

  • Keep the source somewhere that isn't referenced by PYTHONPATH (e.g. ~/projects/myproject)
  • Write a simple setuptools or distutils script for installing it (see Python distutils - does anyone know how to use it?)
  • Use the virtualenv package to create a dev virtual environment with the --no-site-packages option - this way your "dev" version won't see any packages installed in the default python installation.
  • (Also make sure your PYTHONPATH doesn't have any of your source directories)

Then, for testing:

  • Activate dev virtual environment
  • Run install script, (usually something like python setup.py build install). Your package ends up in /path/to/dev_virtualenv/lib/python2.x/site-packages/
  • Test, break, fix, repeat

And, for production:

  • Make sure dev virtualenv isn't activated
  • Run install script
  • All good to go, the "dev" version is hidden away in a virtual environment that production can't see...
  • ...And there's no (direct) messing around with PYTHONPATH

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... ;)

生生漫 2024-10-04 10:23:16

您可以将 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 the PYTHONPATH to point to that code.

(Is that too simplistic? Have I missed something?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文