Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing scripts that require a virtualenv to be activated

I have virtualenv and virtualenvwrapper installed on a shared Linux server with default settings (virtualenvs are in ~/.virtualenvs). I have several Python scripts that can only be run when the correct virtualenv is activated.

Now I want to share those scripts with other users on the server, but without requiring them to know anything about virtualenv... so they can run python scriptname or ./scriptname and the script will run with the libraries available in my virtualenv.

What's the cleanest way to do this? I've toyed with a few options (like changing the shebang line to point at the virtualenv provided interpreter), but they seem quite inflexible. Any suggestions?


Edit: This is a development server where several other people have accounts. However, none of them are Python programmers (I'm currently trying to convert them). I just want to make it easy for them to run these scripts and possibly inspect their logic, without exposing non-Pythonistas to environment details. Thanks.

like image 460
Mzzzzzz Avatar asked Feb 12 '10 17:02

Mzzzzzz


People also ask

How do you share virtual environments?

Simply copy the folder related to the virtal environment to your other computer or sync the folder using rsyncd to avoid having several different versions. The virtual environment folders can be found in ~/. virtualenvs on a standard Ubuntu installation.

What does virtualenv activate do?

virtualenv is a tool for creating isolated Python environments containing their own copy of python , pip , and their own place to keep libraries installed from PyPI. It's designed to allow you to work on multiple projects with different dependencies at the same time on the same machine.

How do I enable scripts in python?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


1 Answers

Use the following magic(5) at the start of the script.

#!/usr/bin/env python 

Change which virtualenv is active and it'll use the python from that virtualenv. Deactivate the virtualenv, it still runs.

like image 139
Chris Dukes Avatar answered Oct 01 '22 02:10

Chris Dukes