Where does the code go to virtualenv?

What directory structure should be used when using virtualenv ? For example, if I create a WSGI application and create a virtualenv called foobar , I would start with a directory structure, for example:

 /foobar /bin {activate, activate.py, easy_install, python} /include {python2.6/...} /lib {python2.6/...} 

Once this environment is created, where will the place be:

  • python files?
  • static files (images / etc)?
  • "custom" packages, for example, available on the Internet but not found in the cheese shop.

in relation to virtualenv directories?

(Suppose I already know where the virtual directories themselves should go .)

+78
python virtualenv project
Nov 23 '09 at 13:31
source share
4 answers

virtualenv provides a python interpreter instance, not an application instance. Usually you did not create application files in directories containing system Python by default, nor is it necessary to find your application in the virtualenv directory.

For example, you may have a project in which you have several applications using the same virtualenv. Or you can test the application using virtualenv, which will later be deployed using the Python system. Or you can package a standalone application where it makes sense to have a virtualenv directory located somewhere in the application folder itself.

So, in general, I do not think that there is one correct answer to the question. And the good thing about virtualenv is that it supports many different use cases: there is no need for the right path.

+63
Nov 23 '09 at 18:44
source share

If you have only a few projects each so often, nothing prevents you from creating a new virtualenv for each of them and putting your packages right inside:

 /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 find to find the activated script that belongs to the project inside.

 $ cd /foobar $ source bin/activate $ python >>> import mypackage1 >>> 

If you decide to be more organized, you should consider placing all your virtual users in one folder and name each of them after the project you are working on.

  /virtualenvs /foobar /bin {activate, activate.py, easy_install, python} /include {python2.6/...} /lib {python2.6/...} /foobar /mypackage1 __init__.py /mypackage2 __init__.py 

That way, you can always start with the new virtualenv when something goes wrong and your project files remain safe.

Another advantage is that some of your projects can use the same virtualenv, so you won’t have to do the same installation over and over again if you have many dependencies.

 $ cd /foobar $ source ../virtualenvs/foobar/bin/activate $ python >>> import mypackage2 >>> 

For users who regularly have to install and reset virtual virtual machines, it would be wise to look at virtualenvwrapper.

 http://pypi.python.org/pypi/virtualenvwrapper 

With virtualenvwrapper you can

 * create and delete virtual environments * organize virtual environments in a central place * easily switch between environments 

You no longer need to worry about where your virtual users are when working on the "foo" and "bar" projects:

  /foo /mypackage1 __init__.py /bar /mypackage2 __init__.py 

Here's how you get started on the foo project:

 $ cd foo $ workon bar foo $ workon foo (foo)$ python >>> import mypackage1 >>> 

Then switching to the bar of the project is just as easy:

 $ cd ../bar $ workon bar (bar)$ python >>> import mypackage2 >>> 

Pretty neat, right?

+43
Nov 23 '09 at 14:33
source share

Since virtualenvs do not move, in my opinion, it is a bad practice to place project files in the virtualenv directory. Virtualenv itself is a generated development / deployment artifact (like a .pyc file), not part of the project; it should be easy to delete and update it at any time or create a new one on a new deployment server, etc.

Many actually use virtualenvwrapper , which almost completely removes virtual virtual threads from your awareness, putting them all side by side, by default in $ HOME / .virtualenvs.

+20
Nov 23 '09 at 20:35
source share

If you give setup.py your project, pip can import it directly from version control.

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 

-e will put the project in myproject/src , but will link it to myproject/lib/pythonX.X/site-packages/ , so any changes you make will be immediately selected in the modules that import it from the local site-packages . The #egg bit tells pip which name you want to refer to the egg package that it creates for you.

If you are not using --no-site-packages , be careful when specifying that you want pip to install in virtualenv with the -e option

+2
Nov 23 '09 at 16:07
source share



All Articles