在同一台计算机上开发和使用相同的Python [英] Developing and using the same Python on the same computer

查看:100
本文介绍了在同一台计算机上开发和使用相同的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Python实用程序模块,以帮助文件下载,存档等.我在虚拟环境中设置了一个项目,并进行了单元测试.当我想在同一台计算机上使用此模块时(基本上是生产"),我将文件移到〜/dev/modules/ mymodule

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

我将所有第三方模块保留在〜/dev/modules/ contrib 下.此 contrib 路径位于我的PYTHONPATH上,但mymodule不是因为我注意到,如果 mymodule 位于我的PYTHONPATH上,则我的单元测试无法区分开发"版本和生产"版本.但是现在,如果我想使用该通用实用程序模块,则必须手动将其添加到PYTHONPATH中.

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.

在同一台计算机上安装开发和生产模块的最佳方法是什么?例如,是否可以动态设置PYTHONPATH?

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?

推荐答案

您可以在sys.path处添加/修改python路径,只需确保第一个路径是当前目录".",因为某些第三方模块依靠从当前模块的目录中导入.

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.

有关python路径的更多信息: http://djangotricks.blogspot.com/2008/09/note -on-python-paths.html

More information on python paths: http://djangotricks.blogspot.com/2008/09/note-on-python-paths.html

这篇关于在同一台计算机上开发和使用相同的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆