Search code examples
bashdockerpipenv

activate a virtual environment and run a command within from a bash script


I am using a bash script as an entry point to a docker container. My goal is to be able to activate the virtual environment within the docker container and then run a command within from the same bash script with no manual input. The relevant bits of the script is as follows

#!/bin/bash
pipenv shell
ln -s /usr/local/lib/python3.7/dist-packages /usr/local/lib/python3.7/site-packages
command to be run within venv
python3

the issue is that when pipenv shell is run the venv is activated and entered but the rest of the command does not run within it. i can manually run commands within the venv and the rest of the script waits for me to exit. How do I specify I want commands run within?


Solution

  • You can use pipenv run to directly run a python or bash script within a virtual-environment. Simply write a script that contains the commands you would like run in your environment script.sh and then do pipenv run script.sh.

    Script.sh:

    ln -s /usr/local/lib/python3.7/dist-packages /usr/local/lib/python3.7/site-packages
    command to be run within venv
    python3

    Then simply: pipenv run script.sh

    Similar to this question