I am trying to write a module file for a program that creates a python virtualenv
. In order to start the virtualenv
, it needs to first run /programs/program-env/bin/activate
. How do I do this in a modulefile
?
Any help will be greatly appreciated.
Note: I tried just putting the above line in the file and it didn't work.
Thanks,
Edit:
I am writing a modulefile to load a program that can only run in a virtualenv
. Normally these modulefiles will set variable names and/or add bin directory to path. Since the above package is somewhat different, I don't know how to proceed. An example module file can be found here.
To install virtualenv, just use pip install virtualenv . To create a virtual environment directory with it, type virtualenv /path/to/directory . Activating and deactivating the virtual environment works the same way as it does for virtual environments in Python 3 (see above).
Search in your applications for the Terminal and open it. Enter into your Desktop folder with the command cd desktop . Type python3 -m venv env to create a virtual environment named env . When the environment is created, the prompt will reappear.
Here is a slightly more complete answer, building on the answers by Donal and betapatch, that allows you to swap between two modules which do similar things:
if { [module-info mode load] || [module-info mode switch2] } {
puts stdout "source /programs/program-env/bin/activate;"
} elseif { [module-info mode remove] && ![module-info mode switch3] } {
puts stdout "deactivate;"
}
Firstly, you need to use source .../activate
rather than just .../activate
.
Secondly, modules
has some horrible logic when swap
ping modules. If you want to module swap foo bar
(remove foo
and load bar
in its place), it actually does the following:
foo: switch1 # prep for remove
foo: remove # actually remove
bar: switch2 # load new module
foo: switch3 # cleanup
foo: remove # happens at the same time as foo switch3
This means that if foo
and bar
are both modulefiles using virtualenvs, the second foo remove
will deactivate
bar
.
The Modules system is pretty strange, since what it's really doing is creating a set of instructions that are evaluated by the calling shell. This means that normal Tcl ways of doing things are often not quite right; it is the caller who needs to run /programs/program-env/bin/activate
, not the Tcl script.
The first thing to try is:
system "/programs/program-env/bin/activate"
However, looking between the lines in the FAQ, I see that you probably would need to do this (with guards):
if {[module-info mode] == "load"} {
puts stdout "/programs/program-env/bin/activate"
}
I have no idea how to reverse the operation (which is part of the point of a module).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With