25

I have a Python virtualenv (created with virtualenvwerapper) in one user account. I would like to use it from another user account on the same host.

How can I set up virtual environments so as to be available to any user on the host? (Primarily Linux / Debian but also macOS.)

codeforester
43.8k21 gold badges123 silver badges159 bronze badges
asked Feb 29, 2012 at 20:40

2 Answers 2

14

Put it in a user-neutral directory, and make it group-readable.

For instance, for libraries, I use /srv/http/share/ for sharing code across web applications.

You could use /usr/local/share/ for normal applications.

answered Feb 29, 2012 at 20:44
Sign up to request clarification or add additional context in comments.

6 Comments

But what's the virtualenv / virtualenvwrapper syntax for using that directory? With virtualenvwrapper, the the command line << workon some_env >> looks for an environment in the user's .virtualenv directory.
What do you mean? You use . bin/activate as usual.
I'm sorry, I don't follow. When I switch into an environment, my command line is << workon some_env >> -- I don't touch bin/activate.
Now I get it. 1: Create environment w/ << virtualenv hothouse -p python2.7 >>. This will create a directory for the environment in the working directory, with a subdir bin/ and script activate. 2: Use the environment with << source [absolute path to environment dir]/bin/activate >>. With proper permissions, presto, the terminal environment now calls that virtualenv. Leave with deactivate. This same call works for environments created with virtualenvwrapper. Thx.
@Flavius you just need to sudo mv ~/.virtualenvs /usr/local/share, mkdir -p /usr/src/venv_projects/, chmod g+rwx /usr/local/share/.virtualenvs, ` chmod g+rwx /usr/local/share/venv_projects` then edit your environment variables for virtualenvwrapper (usually in .bashrc before source virtualenvwrapper.sh). your new bashrc should have lines like export PROJECT_HOME="/usr/src/venv" and export WORKON_HOME="/usr/src/venv. Once you (or others in your group) log in (assuming they've put these lines in their bashrc too!) they can use the workon and mkproject commands as usual
|
11

I had to do this for workmates. The @Flavius answer worked great once I added a few commands to handle virtualenvwrapper. You need to put your venvs and your WORKON projects folder some place you and your boss/friend can find and use.

sudo mkdir -p /usr/local/share
sudo mv ~/.virtualenvs /usr/local/share
sudo mkdir -p /usr/src/venv/

Assuming you want everyone on the machine to be able to both mkproject and workon:

chmod a+rwx /usr/local/share/.virtualenvs
chmod a+rwx /usr/src/venv

Otherwise chown and chmod to match your security requirements.

If you have any hooks or scripts that expect ~/.virtualenvs to be in the normal place, you better symlink it (on both your user account and your friend's)

ln -s /usr/local/share/.virtualenvs ~/.virtualenvs

Then modify your (and your friend's) .bashrc file to let virtualenvwrapper know where you moved things. Your bashrc should have something like this:

export PROJECT_HOME="/usr/src/venv/"
export WORKON_HOME="/usr/local/share/.virtualenvs"
export USR_BIN=$(dirname $(which virtualenv))
if [ -f $USR_BIN/virtualenvwrapper.sh ]; then
 source $USR_BIN/virtualenvwrapper.sh
else
 if [ -f /usr/bin/virtualenvwrapper.sh ]; then
 source /usr/bin/local/virtualenvwrapper.sh
 else
 echo "Can't find a virtualenv wrapper installation"
 fi 
fi

Once you log out and back in (or just source ~/.bashrc you should be good to go with commands like mkproject awesome_new_python_project and workon awesome_new_python_project.

As a bonus, add hooks to load the project folder in sublime every time your workon.

answered Jan 21, 2014 at 18:22

2 Comments

I can't seem to make it work (a module cannot be found when setting up as you suggested)—what is the role of mkdir -p /usr/src/venv—is that dir used for anything?
@pepe /usr/src/venv is probably unnecessary. I don't know why I originally did it that way.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.