How to run Pipenv in a Docker Container, or using Crontab
When you run pipenv shell, you will notice a link in you console terminal output with a dot (.) symbol.
This symbol is the shell command for “source”.
If you haven’t installed pipenv here is the setup:
# Install Pipenv on Ubuntu / Debian (Works on Docker) apt install pipenv -y pip3 install --upgrade pip pip3 install --upgrade pipenv touch Pipfile export SHELL=/bin/bash python3 -m pipenv --python 3.8 # CentOS / Red Hat yum install epel-release yum install python3 python3-pip pip3 install pipenv touch Pipfile pipenv install
Locate your activate bin file:
find $HOME/.local/share/virtualenvs/ | grep 'activate$'
Now you can use that activate bin inside your cron jobs as follow:
# these two commands are the same . /home/admin/.local/share/virtualenvs/private-3Qz0_VCC/bin/activate # or source /home/admin/.local/share/virtualenvs/private-3Qz0_VCC/bin/activate
This gets your inside your python pipenv shell, where running “python” does it’s magic.
For example, here’s how to run Django
Type “crontab -e” and enter the cron job
source /home/admin/.local/share/virtualenvs/private-3Qz0_VCC/bin/activate && cd /home/user/ && nohup python manage.py runserver 127.0.0.1:9988 &
Here are some examples to help you understand
Here’s a fully automated Docker way:
# in any ubuntu image RUN apt update -y RUN apt install pipenv -y RUN pip3 install --upgrade pip RUN pip3 install --upgrade pipenv WORKDIR /opt RUN export SHELL=/bin/bash RUN python3 -m pipenv --python 3.8
If you receive this error:
File "/usr/lib/python3/dist-packages/pipenv/project.py", line 475, in create_pipfile config_parser = ConfigOptionParser(name=self.name) File "/usr/lib/python3/dist-packages/pipenv/vendor/pip9/baseparser.py", line 149, in __init__ assert self.nameAssertionError
you can try two things
export SHELL=/bin/bash pipenv shell
touch pipfile pipenv shell