What sort of directory structure should one follow when using virtualenv
? For instance, if I were building a WSGI application and created a virtualenv called foobar
I would start with a directory structure like:
使用virtualenv时应该遵循什么样的目录结构?例如,如果我正在构建一个WSGI应用程序并创建一个名为foobar的虚拟环境v,我将从以下目录结构开始:
/foobar
/bin
{activate, activate.py, easy_install, python}
/include
{python2.6/...}
/lib
{python2.6/...}
Once this environment is created, where would one place their own:
一旦这种环境被创造出来,人们会把自己放在哪里:
- python files?
- python文件?
- static files (images/etc)?
- 静态文件(图片/等)?
- "custom" packages, such as those available online but not found in the cheese-shop?
- “自定义”包装,比如在网上可以买到但在乳酪店找不到的那种?
in relation to the virtualenv
directories?
关于virtualenv目录?
(Assume I already know where the virtualenv directories themselves should go.)
(假设我已经知道virtualenv目录本身应该去哪里。)
4 个解决方案
#1
64
virtualenv
provides a python interpreter instance, not an application instance. You wouldn't normally create your application files within the directories containing a system's default Python, likewise there's no requirement to locate your application within a virtualenv directory.
virtualenv提供了python解释器实例,而不是应用程序实例。您通常不会在包含系统默认Python的目录中创建应用程序文件,同样也不需要在virtualenv目录中定位应用程序。
For example, you might have a project where you have multiple applications using the same virtualenv. Or, you may be testing an application with a virtualenv that will later be deployed with a system Python. Or, you may be packaging up a standalone app where it might make sense to have the virtualenv directory located somewhere within the app directory itself.
例如,您可能有一个项目,其中有多个应用程序使用相同的virtualenv。或者,您可能正在使用一个virtualenv测试一个应用程序,该应用程序稍后将被部署到一个系统Python中。或者,您可能正在打包一个独立的应用程序,在那里,让virtualenv目录位于应用程序目录本身的某个位置是有意义的。
So, in general, I don't think there is one right answer to the question. And, a good thing about virtualenv
is that it supports many different use cases: there doesn't need to be one right way.
所以,总的来说,我认为这个问题没有一个正确的答案。并且,关于virtualenv的一个好处是它支持许多不同的用例:不需要有一种正确的方法。
#2
48
If you only have a few projects every so often, nothing stops you from creating a new virtualenv for each one, and putting your packages right inside:
如果你只是偶尔有几个项目,没有什么能阻止你为每一个项目创建一个新的virtualenv,并且把你的包放在里面:
/foobar
/bin
{activate, activate.py, easy_install, python}
/include
{python2.6/...}
/lib
{python2.6/...}
/mypackage1
__init__.py
/mypackage2
__init__.py
The advantage of this approach is that you can always be sure to find find the activate script that belongs to the project inside.
这种方法的优点是,您可以始终确保找到属于内部项目的激活脚本。
$ cd /foobar
$ source bin/activate
$ python
>>> import mypackage1
>>>
If you decide to be a bit more organized, you should consider putting all your virtualenvs into one folder, and name each of them after the project you are working on.
如果您决定更有条理一些,您应该考虑将所有的virtualenv放在一个文件夹中,并在您正在处理的项目之后为它们分别命名。
/virtualenvs
/foobar
/bin
{activate, activate.py, easy_install, python}
/include
{python2.6/...}
/lib
{python2.6/...}
/foobar
/mypackage1
__init__.py
/mypackage2
__init__.py
This way you can always start over with a new virtualenv when things go wrong, and your project files stay safe.
这样,当出现问题时,您可以从头开始使用新的virtualenv,并且您的项目文件保持安全。
Another advantage is that several of your projects can use the same virtualenv, so you don't have to do the same installation over and over if you have a lot of dependencies.
另一个优点是,您的几个项目可以使用相同的virtualenv,因此如果您有很多依赖项,您不必反复执行相同的安装。
$ cd /foobar
$ source ../virtualenvs/foobar/bin/activate
$ python
>>> import mypackage2
>>>
For users that regularly have to set up and tear down virtualenvs it would make sense to look at virtualenvwrapper.
对于经常需要设置和销毁virtualenenv的用户来说,查看virtualenvwrapper是有意义的。
http://pypi.python.org/pypi/virtualenvwrapper
With virtualenvwrapper you can
使用virtualenvwrapper可以
* create and delete virtual environments
* organize virtual environments in a central place
* easily switch between environments
You no more have to worry about where your virtualenvs are when working on the projects "foo" and "bar":
当你在做“foo”和“bar”项目时,你不再需要担心你的虚拟人在哪里:
/foo
/mypackage1
__init__.py
/bar
/mypackage2
__init__.py
This is how you start working on project "foo":
这就是你开始进行“foo”项目的方式:
$ cd foo
$ workon
bar
foo
$ workon foo
(foo)$ python
>>> import mypackage1
>>>
Then switching to project "bar" is as simple as this:
那么切换到项目“bar”就是这么简单:
$ cd ../bar
$ workon bar
(bar)$ python
>>> import mypackage2
>>>
Pretty neat, isn't it?
很整洁的,不是吗?
#3
24
Because virtualenvs are not relocatable, in my opinion it is bad practice to place your project files inside a virtualenv directory. The virtualenv itself is a generated development/deployment artifact (sort of like a .pyc file), not part of the project; it should be easy to blow it away and recreate it anytime, or create a new one on a new deploy host, etc.
因为virtualenv是不可重定位的,所以我认为将项目文件放在virtualenv目录中是不好的做法。virtualenv本身是一个生成的开发/部署工件(类似于.pyc文件),而不是项目的一部分;应该很容易删除它并在任何时候重新创建它,或者在新的部署主机上创建一个新的,等等。
Many people in fact use virtualenvwrapper, which removes the actual virtualenvs from your awareness almost completely, placing them all side-by-side in $HOME/.virtualenvs by default.
许多人实际上使用virtualenvwrapper,它几乎完全从你的意识中删除了真实的virtualenv包装,将它们放在$HOME/中并排放置。默认virtualenv。
#4
2
If you give your project a setup.py
, pip can import it from version control directly.
如果你给你的项目设置。py, pip可以直接从版本控制中导入。
Do something like this:
这样做:
$ virtualenv --no-site-packages myproject
$ . myproject/bin/activate
$ easy_install pip
$ pip install -e hg+http://bitbucket.org/owner/myproject#egg=proj
The -e
will put the project in myproject/src
, but link it to myproject/lib/pythonX.X/site-packages/
, so any changes you make will get picked up immediately in modules that import it from your local site-packages
. The #egg
bit tells pip what name you want to give to the egg package it creates for you.
e将把项目放在myproject/src中,但是将它链接到myproject/lib/pythonX。X/site-packages/,您所做的任何更改都将在从本地站点-package导入它的模块中得到。#egg bit告诉pip你想给它为你创建的蛋包取什么名字。
If you don't use --no-site-packages
, be careful to specify that you want pip to install into the virtualenv with the -E
option
如果不使用——无站点包,请小心指定您希望pip使用-E选项安装到virtualenv中
#1
64
virtualenv
provides a python interpreter instance, not an application instance. You wouldn't normally create your application files within the directories containing a system's default Python, likewise there's no requirement to locate your application within a virtualenv directory.
virtualenv提供了python解释器实例,而不是应用程序实例。您通常不会在包含系统默认Python的目录中创建应用程序文件,同样也不需要在virtualenv目录中定位应用程序。
For example, you might have a project where you have multiple applications using the same virtualenv. Or, you may be testing an application with a virtualenv that will later be deployed with a system Python. Or, you may be packaging up a standalone app where it might make sense to have the virtualenv directory located somewhere within the app directory itself.
例如,您可能有一个项目,其中有多个应用程序使用相同的virtualenv。或者,您可能正在使用一个virtualenv测试一个应用程序,该应用程序稍后将被部署到一个系统Python中。或者,您可能正在打包一个独立的应用程序,在那里,让virtualenv目录位于应用程序目录本身的某个位置是有意义的。
So, in general, I don't think there is one right answer to the question. And, a good thing about virtualenv
is that it supports many different use cases: there doesn't need to be one right way.
所以,总的来说,我认为这个问题没有一个正确的答案。并且,关于virtualenv的一个好处是它支持许多不同的用例:不需要有一种正确的方法。
#2
48
If you only have a few projects every so often, nothing stops you from creating a new virtualenv for each one, and putting your packages right inside:
如果你只是偶尔有几个项目,没有什么能阻止你为每一个项目创建一个新的virtualenv,并且把你的包放在里面:
/foobar
/bin
{activate, activate.py, easy_install, python}
/include
{python2.6/...}
/lib
{python2.6/...}
/mypackage1
__init__.py
/mypackage2
__init__.py
The advantage of this approach is that you can always be sure to find find the activate script that belongs to the project inside.
这种方法的优点是,您可以始终确保找到属于内部项目的激活脚本。
$ cd /foobar
$ source bin/activate
$ python
>>> import mypackage1
>>>
If you decide to be a bit more organized, you should consider putting all your virtualenvs into one folder, and name each of them after the project you are working on.
如果您决定更有条理一些,您应该考虑将所有的virtualenv放在一个文件夹中,并在您正在处理的项目之后为它们分别命名。
/virtualenvs
/foobar
/bin
{activate, activate.py, easy_install, python}
/include
{python2.6/...}
/lib
{python2.6/...}
/foobar
/mypackage1
__init__.py
/mypackage2
__init__.py
This way you can always start over with a new virtualenv when things go wrong, and your project files stay safe.
这样,当出现问题时,您可以从头开始使用新的virtualenv,并且您的项目文件保持安全。
Another advantage is that several of your projects can use the same virtualenv, so you don't have to do the same installation over and over if you have a lot of dependencies.
另一个优点是,您的几个项目可以使用相同的virtualenv,因此如果您有很多依赖项,您不必反复执行相同的安装。
$ cd /foobar
$ source ../virtualenvs/foobar/bin/activate
$ python
>>> import mypackage2
>>>
For users that regularly have to set up and tear down virtualenvs it would make sense to look at virtualenvwrapper.
对于经常需要设置和销毁virtualenenv的用户来说,查看virtualenvwrapper是有意义的。
http://pypi.python.org/pypi/virtualenvwrapper
With virtualenvwrapper you can
使用virtualenvwrapper可以
* create and delete virtual environments
* organize virtual environments in a central place
* easily switch between environments
You no more have to worry about where your virtualenvs are when working on the projects "foo" and "bar":
当你在做“foo”和“bar”项目时,你不再需要担心你的虚拟人在哪里:
/foo
/mypackage1
__init__.py
/bar
/mypackage2
__init__.py
This is how you start working on project "foo":
这就是你开始进行“foo”项目的方式:
$ cd foo
$ workon
bar
foo
$ workon foo
(foo)$ python
>>> import mypackage1
>>>
Then switching to project "bar" is as simple as this:
那么切换到项目“bar”就是这么简单:
$ cd ../bar
$ workon bar
(bar)$ python
>>> import mypackage2
>>>
Pretty neat, isn't it?
很整洁的,不是吗?
#3
24
Because virtualenvs are not relocatable, in my opinion it is bad practice to place your project files inside a virtualenv directory. The virtualenv itself is a generated development/deployment artifact (sort of like a .pyc file), not part of the project; it should be easy to blow it away and recreate it anytime, or create a new one on a new deploy host, etc.
因为virtualenv是不可重定位的,所以我认为将项目文件放在virtualenv目录中是不好的做法。virtualenv本身是一个生成的开发/部署工件(类似于.pyc文件),而不是项目的一部分;应该很容易删除它并在任何时候重新创建它,或者在新的部署主机上创建一个新的,等等。
Many people in fact use virtualenvwrapper, which removes the actual virtualenvs from your awareness almost completely, placing them all side-by-side in $HOME/.virtualenvs by default.
许多人实际上使用virtualenvwrapper,它几乎完全从你的意识中删除了真实的virtualenv包装,将它们放在$HOME/中并排放置。默认virtualenv。
#4
2
If you give your project a setup.py
, pip can import it from version control directly.
如果你给你的项目设置。py, pip可以直接从版本控制中导入。
Do something like this:
这样做:
$ virtualenv --no-site-packages myproject
$ . myproject/bin/activate
$ easy_install pip
$ pip install -e hg+http://bitbucket.org/owner/myproject#egg=proj
The -e
will put the project in myproject/src
, but link it to myproject/lib/pythonX.X/site-packages/
, so any changes you make will get picked up immediately in modules that import it from your local site-packages
. The #egg
bit tells pip what name you want to give to the egg package it creates for you.
e将把项目放在myproject/src中,但是将它链接到myproject/lib/pythonX。X/site-packages/,您所做的任何更改都将在从本地站点-package导入它的模块中得到。#egg bit告诉pip你想给它为你创建的蛋包取什么名字。
If you don't use --no-site-packages
, be careful to specify that you want pip to install into the virtualenv with the -E
option
如果不使用——无站点包,请小心指定您希望pip使用-E选项安装到virtualenv中