问题
I've got two python projects in a separate folders. And I also have a common code which I use in both of my projects.
What is the best way to organize to them on filesystem to use distutils with setup.py to build them?
My first idea was to use the structure like this:
workspace
|
|- common lib
| |
| `- ...
|
|- project 1
| |
| |- ...
| `- setup.py
|
`- project 2
|
|- ...
`- setup.py
and modify the python path environment variable in projects to find the code, but I've run into problems when I write setup.py file. If I choose the relative path for my common package (..\common lib), then distutils put the common lib files outside of the archive.
Is there any good practice to place reusable code and build projects at the same time?
Thanks!
回答1:
I would check out the distribution utils for python (start here). You can set each project as its own separate package and then install each package on your machine as you would any other python package. In addition, you can symlink the package on your local machine, such that if your projects are co-dependent, any changes in project A can immediately be accessed by code in other projects without having to re-install project A after making changes.
In general keeping things separate makes it easier to manage.
Although, it would be helpful to know what these projects are. Are they all separate libraries or apps you are writing?
回答2:
The package_dir
option in distutils/setup-tools
can insert a package (in your case, common_lib
) at a location within the distribution tree that is different from the source tree (https://docs.python.org/2/distutils/setupscript.html#listing-whole-packages).
But as far as I can tell this only works properly when creating a binary distribution (setup.py bdist); I ran into this problem too here: Inconsistent behaviour of bdist vs sdist when distributing a Python package.
来源:https://stackoverflow.com/questions/34472075/how-to-place-a-common-code-for-different-projects