在使用virtualenv时,在pythonpath中包含Git子模块 [英] Including Git submodules on pythonpath when using virtualenv

查看:148
本文介绍了在使用virtualenv时,在pythonpath中包含Git子模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django项目中使用Git进行版本控制。



尽可能地,不是项目本身的所有源代码,而是项目所依赖的源代码作为Git子模块引入。它们生活在一个 lib 目录下,并且必须包含在python路径中。目录/文件布局如下所示:

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

你会说什么是将这些lib包装到python路径上的最佳做法?



我正在使用virtualenv,所以我可以轻松地将这些库链接到virtualenv的site-packages目录。但是,这将使virtualenv与这个具体的项目相关联。我的理解是,virtualenv不应该依赖于我的文件。相反,我的文件应该依赖于virtualenv。



我正在考虑对本项目的不同本地副本使用相同的virtualenv,但是如果我这样做,我将失去该功能。任何更好的主意如何处理?






更新:



最好的解决方案是让pip管理所有的依赖项。



但是,这意味着无法使用git子模块,因为不能正确处理相对路径。因此,外部依赖关系必须存在于virtualenv(通常为:my_env / src / a_python_module)上。



我还是更喜欢使用子模块,让一些依赖项生活在我的项目树上。这对我来说更有意义了,因为我已经需要把这些repos转换成一些它们,并且将来可能需要更改它们。

解决方案

使用

 <$>将所有已安装的软件包转储到需求文件(requirements.txt查看标准命名) c $ c> pip freeze>每当你需要一个新鲜的virtualenv,你只需要做:


$ b


$ b

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

安装 - 如果要更新到更新的软件包,那么r requirements.txt也可以很好用。



只要保持require.txt与您的软件包同步(每次发生更改时都会运行点击冻结)你完成了,无论你有多少virtualenv。



注意:如果你需要在一个包上做一些开发,你可以使用-e(可编辑)param,这样你可以编辑包,你不需要卸载/安装每次你想测试新的东西:)


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.

解决方案

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 :)

这篇关于在使用virtualenv时,在pythonpath中包含Git子模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆