loading...

Python: Specify a project directory with virtualenvwrapper

adamlombard profile image Adam Lombard Updated on ・1 min read

If you're using virtualenvwrapper to manage your Python environments, you can specify your project directory in your environment settings. Doing so will automatically cd you into your project when you open its environment — a nice little time-saving feature.

When creating our environment, we can use the -a flag to specify the project directory:

$ mkvirtualenv -a [/path/to/project] [name-of-environment]

If, for example, we want to create an environment for a tutorial project at the root of our home directory on a Mac, we would execute:

$ mkvirtualenv -a ~/tutorial tutorial-env

The next time we start our virtual environment, it will automatically cd us to the proper directory:

$ workon tutorial-env

(tutorial-env) ~/tutorial $ 

(Your prompt may look different, depending on your settings.)

But, what if we've already created a project and environment without this setting? In that case, we can use setvirtualenvproject to change this setting:

$ setvirtualenvproject [/path/to/environment] [/path/to/project]

On a Mac, virtualenvwrapper places our virtualenv environments in the ~/.virtualenvs directory. So, per our example above, we would execute:

$ setvirtualenvproject ~/.virtualenvs/tutorial-env/ ~/tutorial

Posted on by:

Discussion

pic
Editor guide