I'd like to alter my $PATH only in a Python virtual environment. Is it possible to have the $PATH change when I activate a virtual environment?
-
Why would you want to do this?no_name– no_name2015-03-05 16:35:56 +00:00Commented Mar 5, 2015 at 16:35
-
I need access to a binary while in this environment, and would prefer not to permanently alter $PATH to get it.Walrus the Cat– Walrus the Cat2015-03-05 17:21:04 +00:00Commented Mar 5, 2015 at 17:21
-
@no_name - i do this to create a test environment. Activating the test environment starts by activating the virtualenv and then adding to the environment (and even mapping drives and running other programs). One stop shopping.tdelaney– tdelaney2015-03-06 16:20:07 +00:00Commented Mar 6, 2015 at 16:20
2 Answers
You can write an activation script that sources virtualenv's activate (on linux, or calls the bat file on windows) and then updates PATH, PYTHONPATH and other environment variables. Use the virtualenv bootstrap hooks to install the script when the virtualenv is created and call it instead of activate.
Comments
Depending on what operating system you are using you could edit the activate file and set an environment variable there. For example, a Windows virtualenv folder has a sub-folder called Scripts. Inside scripts is the activate.bat file. Edit activate.bat and alter the path variable. One thing to consider though, is you might want to save the original path variable in another temporary environment variable and restore from that temporary environment variable in the deactivate.bat file.