[Python](EN) Run pyenv-virtualenv python in crontab
Add python file which runs in pyenv-virtualenv to crontab and run it.
Environment and Prerequisite
- Linux
- pyenv
- pyenv-virtualenv
Post Background
Background
- To isolate dependency in python versions, people use
pyenvandpyenv-virtualenv. By using those softwares we can make virtual environment which can run python code file. I encounter run python code file in crontab which runs in virtual environment. - It can be used in not only
crontabbut alsosystemd’s service and shell script.
Prior knowledge
Python Module and Path
- When running python code file, there are many important things to care. However this post focus on python module path and which python you use.
sys.path
sys.path: A list of strings that specifies the search path for modules.- Refer
sys.pathwhen load python module usingimport.
>>> import sys
>>> sys.path
['', '/home/twpower/.pyenv/versions/3.6.2/lib/python36.zip', '/home/twpower/.pyenv/versions/3.6.2/lib/python3.6', '/home/twpower/.pyenv/versions/3.6.2/lib/python3.6/lib-dynload', '/home/twpower/.pyenv/versions/jupyter-notebook/lib/python3.6/site-packages']
pyenv versions
- Show number of python versions which installed by
pyenv - Also includes
pyenv-virtualenv’s virtual environment.
$ pyenv versions
* system (set by /home/twpower/.pyenv/version)
3.5.2
3.5.3
3.6.1
3.6.2
3.6.2/envs/jupyter-notebook
3.6.2/envs/test
jupyter-notebook
test
pyenv path
pyenv versionspython and modules are located in$(pyenv root)/versionspath.
$ ls /home/twpower/.pyenv/versions/
3.5.2 3.5.3 3.6.1 3.6.2 jupyter-notebook test
How To Apply
Method1 - Use virtual environment’s python directly
- Each
pyenv-virtualenvhas its own python. So its python path is different and alsosys.pathis different. - Use python code file directly which is in
pyenv-virtualenv. - Path:
[pyenv root]/versions/[virtualenv name]/bin/[python version]
crontab
30 20 * * * /home/twpower/.pyenv/versions/jupyter-notebook/bin/python3.6 /home/twpower/test.py
Method2 - Use “pyenv activate” in script
- Add
pyenv activateto make virtual environment in shell script and run python code.
test.sh
#!/bin/bash
source /home/twpower/.bash_profile
source /home/twpower/.bashrc
pyenv activate jupyter-notebook
python /home/twpower/test.py
crontab
30 20 * * * /home/twpower/test.sh