virtualenvwrapper: how to update project path?

Michael B picture Michael B · Jul 23, 2016 · Viewed 8.2k times · Source

When I move a project folders I have to manually update the project path in the .project file to get the workon command to work. Is it possible to update the path automatically?

Answer

Bono picture Bono · Mar 17, 2017

According to the docs you can use setvirtualenvproject. This will automatically move you to the project folder if you use the workon command:

bono~$: setvirtualenvproject ~/.virtualenvs/your-virtual-env/ ~/path/to/your/project

Or, as beruic mentioned, it's easier to activate the environment and move to your desired working directory first. Please note that this not always work on my system, but it is a lot easier if it does work for you:

$ workon your-virtual-env
$ cd ~/path/to/your/project
$ setvirtualenvproject

In the future it might also be handy to specify the project path for the virtualenv on creation. You have to specify the project with the -a flag.

The -a option can be used to associate an existing project directory with the new environment.

You can use it something like this:

bono~$: cd ~/your/project
bono~$: mkvirtualenv my-project -a $(pwd)

The next time you use workon you will automatically be moved to your project directory.

Alternative


If you want to automatically detect directory changes and set the correct virtualenvwrapper then and there you can have a look at this post. It's a bit too expansive to go into detail here, but I think you can find what you're looking for there if that's what you meant.