Including Git submodules on pythonpath when using

2019-07-16 16:10发布

问题:

I'm using Git for version control on a Django project.

As much as possible, all the source-code that is not part of the project per se, but that the project depends on, is brought in as Git submodules. These live on a lib directory and have to be included on python path. The directory/files layout looks like:

.git
docs
lib
my_project
    apps
    static
    templates
    __init__.py
    urls.py
    manage.py
    settings.py
    .gitmodules
README

What, would you say, is the best practice for including the libs on python path?

I am using virtualenv, so I could easily sym-link the libraries to the virtualenv's site-packages directory. However, this will tie the virtualenv to this specific project. my understanding is that the virtualenv should not depend on my files. instead, my files should depend on the virtualenv.

I was thinking of using the same virtualenv for different local copies of this project, but if I do things this way I will lose that capability. Any better idea how to approach this?


Update:

The best solution turned out being to let pip manage all the dependencies.

However, this means not being able to use git submodules, as pip can't yet handle relative paths properly. So, external dependencies will have to live on the virtualenv (typically: my_env/src/a_python_module).

I'd still prefer to use submodules, to have some of the dependencies living on my project tree. This makes more sense to me as I've already needed to fork those repos to change some bits of them, and will likely have to change them some more in the future.

回答1:

dump all your installed packages in a requirement file (requirements.txt looks the standard naming) using

pip freeze > requirements.txt

everytime you need a fresh virtualenv you just have to do:

virtualenv <name> --no-site-packages
pip install -r requirements.txt

the install -r requirements.txt works great also if you want to update to newer packages

just keep requirements.txt in sync with your packages (by running pip freeze every time something changes) and you're done, no matter how many virtualenv you have.

NOTE: if you need to do some development on a package you can install that using the -e (editable) param, this way you can edit the package and you don't have to uninstall/install every time you want to test new stuff :)