4

Hi I've been reading a lot about virtual environments but I don't seem to get one thing.

So I have my path like this:

../my_app/
../my_app/app.py
..etc

Where should I put the virtual environment? Inside my_appas /my_app/venv/bin,include,lib? or at the same level as my_app

/my_app/
/venv/

I don't understand if the location matters or if by using activate it will reference it instead of going to the main environment.

I hope this question makes sense.

Thanks!

villancikos
  • 519
  • 1
  • 6
  • 13
  • Consider `virtualenvwrapper` -- it has a few more features and is easier to use. http://virtualenvwrapper.readthedocs.org/en/latest/ – johntellsall Jun 29 '14 at 23:37

2 Answers2

3

I recommend utilizing the root directory which virtualenv creates as the root directory for your source files. Virtual-envirments are designed to be tied to a project, not shared between different projects. Example, lets say I write a lot of code in my ~/school directory. I'd cd to ~/school, do virtualenv ENV. Now I have an ENV directory in which to keep my source files and dependencies for the project. So you can create a ~/school/ENV/source folder in which to keep all your source folders. And all your virtual-environment files are close to your program, easily accessible in the ENV directory.

EDIT:

To address one part of your question: As long as you keep track of your environment, and you source bin/activate before running your python programs and installing dependencies with pip, you can install your virtual environment anywhere.

Blake G
  • 581
  • 7
  • 24
  • Just for summary. Virtual Environments are used as a reference rather than a containing folder, right? – villancikos Jul 01 '14 at 23:03
  • I'm not sure what you mean when you say "virtual environments are used as a reference." I'd be happy to respond if you can give a bit more detail. – Blake G Jul 02 '14 at 23:21
  • 1
    What I mean is that a virtual environment just works as a place to save all the installations made by pip. And when you activate it the "terminal" references that location. So it doesn't matter at the end where that folder is as long as you activate it when you need it. Therefore your virtual environments and your projects/apps can be in different places as long as you remember which venv is tied to what projecr/app – villancikos Jul 03 '14 at 00:02
2

I don't understand if the location matters or if by using activate it will reference it instead of going to the main environment.

It doesn't matter, as activate will take care of the paths correctly, but you shouldn't put your virtual environment in your source, as it is not part of your application (and its not portable). Instead, create a file with your requirements and put that under your source control.

You should put it in any directory other than your source code. The activate script will make sure the paths point to the right places.

Here is an example:

$ virtualenv /home/somedir/envs/myenv
... # some output
$ source /home/somedir/envs/myenv/bin/activate
(myenv) $ mkdir /home/somedir/projects
(myenv) $ cd /home/somedir/projects
(myenv) projects $ 

As you can see, the virtual environment is in the envs directory and is called myenv. Your source is in /home/somedir/projects. Type deactivate to exit your virtual environment.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • I completely disagree. virtual-environments ARE portable. That what `pip freeze > requirements.txt` is for. It allows to to replicate your virtual-environment anywhere. And virtual-environments are closely coupled with individual projects, so why would you keep it anywhere else besides with your source/project files? – Blake G Jun 30 '14 at 01:09
  • The requirements file is a plain text file that you don't need a virtual environment to create. You cannot move virtual environments between machines and expect them to work, you can take my word for it - or just wait till you have a colleague who is working on Windows and let him update your repository with his virtual environment and then wait to run it on your Linux or OSX machine. – Burhan Khalid Jun 30 '14 at 05:31
  • You don't transport the actual virtual-environment. You re-create it with the requirements.txt file. You ignore the virtual-environment in you SCMer, and recreate them locally. – Blake G Jun 30 '14 at 13:20
  • Which is why I said don't put it in with the source code. Put the requirements file with your source code, not the environment itself. – Burhan Khalid Jun 30 '14 at 13:22