为多个开发人员管理sys.path [英] Managing sys.path for multiple developers
问题描述
我面临的问题很小但很烦人:
The problem I'm facing is small but annoying:
一位同事正在版本控制系统X(VCS-X)中从事一个项目. 另一个同事正在另一个版本控制系统Y中工作,并使用来自X的程序包.
A colleague is working on one project in version control system X (VCS-X). Another colleague is working in another version control system Y and uses the packages from X.
不幸的是,VCS-X中的同事使用本地导入,并在其代码中使用sys.path.append('trunk/my_location')
修改了他的路径.
Unfortunately colleague in VCS-X uses local import and modifies his path using sys.path.append('trunk/my_location')
in their code.
我的观点是,这是错误的做法,因为X的同事强迫Y的同事在运行代码之前先对其进行编辑,只是因为他们的仓库名称不同.
My view is that this is wrong practice as colleagues in X forces colleague Y to edit the code prior to being able to run it, merely because their repo is named differently.
应如何管理这些依赖关系?
How should these dependencies be managed?
开发者X :
>>> sys.path.append('my_repo/my_location')
>>> from my_location import toolbox
>>> nosetests -v
toolbox.test1 ... ok
toolbox.test2 ... ok
...
开发者Y :
第1步:
>>> nosetests -v
toolbox.test1 ... fail
...
第2步:
>>> sys.path.append('my_repo/my_location')
>>> from my_location import toolbox
Import error: No such package.
第3步:
>>> sys.path.append('my_colleagues_repo/my_location')
>>> from my_location import toolbox
>>> nosetests -v
toolbox.test1 ... ok
toolbox.test2 ... ok
"...感叹;代码正在运行..."
"...Sigh follows; the code is working ..."
推荐答案
没人应该做sys.path.append
!这是您首先要解决的工作流程问题.
Nobody should be doing sys.path.append
! This is a workflow problem that you should address first and foremost.
在这种情况下,将toolbox
打包到发行版中是适当的.只是想使用 toolbox
中的代码(即通过导入语句或命令行脚本)的开发人员,将执行:
In this situation it will be appropriate to package the toolbox
into a distribution. The developer who just wants to use the code from toolbox
, i.e. via an import statement or command line script, will execute:
pip install --user toolbox
想要在toolbox
代码上运行 的开发人员也应该使用pip install.但是,此开发人员应克隆存储库,创建/激活虚拟环境,然后执行:
The developer who wants to work on the toolbox
code should also be using pip install. However, this developer should clone the repo, create/activate a virtual environment, and execute:
pip install --editable .
在两种情况下,pip都会以正确的方式为您整理必要的sys.path
内容.
In both situations, pip will sort out the necessary sys.path
stuff for you in the correct way.
请遵循 PyPA Python打包用户指南,以获取有关如何创建发行版的详细信息.
Follow the PyPA Python Packaging User Guide for the details on how to create a distribution.
这篇关于为多个开发人员管理sys.path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!