Make virtualenv and activate it with shell script

5,502

Working with Ubuntu (and most other Linux distros I’d say) it’s safe to use the absolute paths as Python is an essential component of the OS:

/usr/bin/python   # or respectively
/usr/bin/python3

If you don’t want to run the systemwide installed Python version, but rather the one first in the calling user’s PATH, instead use:

/usr/bin/env python   # or respectively
/usr/bin/env python3

Further reading about this alternative

Share:
5,502
Kankarollo
Author by

Kankarollo

Updated on September 18, 2022

Comments

  • Kankarollo
    Kankarollo over 1 year

    I want to make shell script that makes virtualenv, activates it, install some libraries and run python script with it.

    But I have problem that I can't activate virtualenv in shell script unless i do "source script.sh" but then python3 doesn't work. How can I do something like this?

    #!/bin/bash
    python3 -m pip install virtualenv
    python3 -m virtualenv virtual
    source virtual/bin/activate
    pip install <some libraries>
    python <filename.py>
    

    I would like to do this without any global paths to python. I want it to work locally.