2

I use virtualenvwrapper to manage my environments. I create my projects with the -a <path-to-project> argument and PROJECT_HOME is not set, because my projects don't share a common path. VIRTUALENVWRAPPER_WORKON_CD is set to 1 though and a valid .project file exist in the virtual environments.

When I use the workon command, it only changes the working directory to the path of the project sometimes, while at other times, the directory stays the same, despite the environment being activated correctly.

So when and how does the directory change trough workon happens? And are there things I have to do / set for it to work?

Brutus
  • 7,139
  • 7
  • 36
  • 41
  • Have you tried running the command setvirtualenvproject in the directory that you want to cd into after you have activated the project? – Bradley Bossard Dec 10 '16 at 20:40
  • 1
    Yes, tried that. And also verified, that the `.project` files contain the correct path. – Brutus Dec 10 '16 at 20:45
  • 1
    This happens after I close my terminal and reopen it again. It seems virtualenvwrapper is not saving the configuration – smac89 Sep 19 '17 at 15:21

2 Answers2

3

I have a small solution albeit kinda hacky

  1. Go to the folder which this variable, $VIRTUALENVWRAPPER_HOOK_DIR is pointing to. You might need to be in a virtual environment to see that variable, but usually it has the same value as $WORKON_HOME
  2. In this folder you should see a script called postactivate
  3. Add this as the last line in the script: cdproject

Now what this does is that every time you type workon <project_name>, this script will run after the virtual environment is activated, and the directory you were working in will switch to the directory for that project.

For more life-cycle hooks, see here!

NOTE

I didn't fully test this to make sure it works no matter how the project was created, but to make sure it works, I recommend creating your virtual environments with mkvirtualenv -a <env_name> or if the environment exists and with the environment activated, go to it's project folder and run setvirtualenvproject. Now the next time you try to do workon ..., the script will kick in and take you to your project folder

Addendum

Since this is a shell script, you can do some fancy tweaks than just that one line. For example you might want to only do something if the activated project has a certain pattern, or part of a certain group of projects. Checkout some of the $VIRTUALENVWRAPPER_* variables to see what other useful information you can get

smac89
  • 39,374
  • 15
  • 132
  • 179
  • This worked. What I don't get, though, is why it sometimes did this automatically, without me ever setting a hook? Don't see the pattern. – nisc Jul 10 '21 at 17:03
1

setvirtualenvproject is now called setprojectdir. To use the current directory write setprojectdir .

ptav
  • 171
  • 1
  • 11
  • Not in my version of virtualenvwrapper ($ pip show virtualenvwrapper Name: virtualenvwrapper Version: 4.8.4) – nisc Jul 10 '21 at 17:04