问题
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