Python: Specify a project directory with virtualenvwrapper

adamlombard

Adam Lombard

Posted on December 30, 2019

Python: Specify a project directory with virtualenvwrapper

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

$ workon tutorial-env

(tutorial-env) ~/tutorial $ 
Enter fullscreen mode Exit fullscreen mode

(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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Was this helpful? Did I save you some time?

🫖 Buy Me A Tea! ☕️


💖 💪 🙅 🚩
adamlombard
Adam Lombard

Posted on December 30, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related