Managing sys.path for multiple developers

泄露秘密 提交于 2019-12-02 13:32:02

问题


The problem I'm facing is small but annoying:

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.

Unfortunately colleague in VCS-X uses local import and modifies his path using sys.path.append('trunk/my_location') in their code.

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?

Example:

Developer X:

>>> sys.path.append('my_repo/my_location')
>>> from my_location import toolbox
>>> nosetests -v
toolbox.test1 ... ok
toolbox.test2 ... ok
...

Developer Y:

Step 1:

>>> nosetests -v
toolbox.test1 ... fail
...

Step 2:

>>> sys.path.append('my_repo/my_location')
>>> from my_location import toolbox
Import error: No such package.

Step 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 ..."


回答1:


Nobody should be doing sys.path.append! This is a workflow problem that you should address first and foremost.

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

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 .

In both situations, pip will sort out the necessary sys.path stuff for you in the correct way.

Follow the PyPA Python Packaging User Guide for the details on how to create a distribution.



来源:https://stackoverflow.com/questions/45964562/managing-sys-path-for-multiple-developers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!