0

On the servers I am working on, there is a virtual environment which can be activated using source /bin/virtualenv-activate. I need this virtual environment because of a command line tool which is accessible only there. Let's call it fancytool. What I would like to do is to use fancytool out of a python script and return its output into a python variable. The python script is not initiated in the virtual environment, so I thought of something like this:

os.system('source /bin/virtualenv-activate')
results = os.popen(fancytool).read()

However, this returns:

sh: 1: source: not found
sh: 1: fancytool: not found

If I enter source /bin/virtualenv-activate in the terminal and then fancytool, everything works fine. How can I achieve this also in a python script?

2
  • Why not just clone the virtual environment and then build your script in it? Commented Feb 19, 2020 at 15:55
  • The script is called from within another script which cannot be called in the virtual environment Commented Feb 19, 2020 at 15:56

1 Answer 1

1

You should add a shebang to the top of the script to activate the env

#!/path/to/venv/bin/python 

# your code here

However relying on the knowledge of the venv within your scripts is considered poor practice

Sign up to request clarification or add additional context in comments.

6 Comments

I tried it. It still returns the error sh: 1: fancytool: not found. Note that fancytool is from another software than python inside the virtual environment. The virtual environment is not a python virtualenv
Well, to me that says your PATH doesn't have said command on it. Try using the full path to the executable
I do use the full path, e.g. #!/bin/virtualenv/python
No. To fancy tool
yes, it does work! But what would be the 'good practice' then?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.