villancikos
villancikos

Reputation: 539

Virtual Environments with Python

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!

Upvotes: 5

Views: 4566

Answers (2)

Blake G
Blake G

Reputation: 581

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.

Upvotes: 3

Burhan Khalid
Burhan Khalid

Reputation: 174748

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.

Upvotes: 3

Related Questions