Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load virtualenv using environmental module file (tcl script)?

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.

like image 404
arnstrm Avatar asked Apr 05 '14 18:04

arnstrm


People also ask

How do I activate virtualenv?

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).

How do I run a virtualenv in terminal?

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.


2 Answers

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 swapping 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.

like image 200
Yossarian Avatar answered Sep 19 '22 07:09

Yossarian


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).

like image 27
Donal Fellows Avatar answered Sep 19 '22 07:09

Donal Fellows