How to run my Python script with Anaconda environment in crontab?
If the Python file only need python (not other library)
56 16 * * * /home/MY_ACTUAL_USERNAME/anaconda3/envs/rapids/bin/python /home/MY_ACTUAL_USERNAME/Documents/h.py
If Python file requires other python libraries that is in the anaconda environment:
- create a SHELL script
nano my_sehell_file_name.sh
- Example what should be inside the file
#!/bin/bash
#conda activate rapids WRONG
source ~/anaconda3/bin/activate MY_ANACONDA_ENVIRONMENT_NAME #correct
#python Documents/my_python_file_name.py WRONG SEPARATLY GO TO FOLER WHTAN EXECUTE EITH python
cd ~/Documents/folder_where_python_file_is/ #correct
python my_python_file_name.py #correct
conda deactivate
- start up corntab by
corntab -e
- ex what you can write to the end of this corntab file
43 21 * * * /home/MY_ACTUAL_USERNAME/my_sehell_file_name.sh
Running python scripts in Anaconda environment through Windows cmd
You could
- Create a
.bat
file (e.g.run_python_script.bat
) with contents shown below. - Create task in "Task Scheduler" to run the
.bat
file.
1.a. The .bat file contents with conda environments
- Check your
<condapath>
. Yourconda.exe
is located at<condapath>/Scripts
. - Put into your .bat file
call "<condapath>\Scripts\activate.bat" <env_name> & cd "<folder_for_your_py_script>" & python <scriptname.py> [<arguments>]
<env_name>
is the name of the conda environment.<folder_for_your_py_script>
is the folder that contains<scriptname.py>
<scriptname.py>
is the script you want to start.[<arguments>]
represent the optional arguments (if you need to give arguments to your script)
1.b. The .bat file contents with venv
"<path_to_python_exe>" "<path_to_python_script>" [<arguments>]
where
<path_to_python_exe>
is the path to your python executable. If you are using a virtual environment (venv
), then use thepython.exe
found in the/venv/Scripts
folder<path_to_python_script>
is the path to your python script.[<arguments>]
represent the optional arguments (if you need to give arguments to your script)
2. Creating task in Task Scheduler
- Go to "Task Scheduler" -> "Create Basic Task"
- Give the name & timing info
- Add to the "Program/Script" the path to your
run_python_script.bat
.
Appendix: Creating venv
with Anaconda
It seems that conda create
command does not create similar virtual environments as python -m venv
command. To create normal python virtual environment with the venv
- Check your
<condapath>
. Yourconda.exe
is located at<condapath>/Scripts
. - Create virtual environment to folder you want (let's call it
venv_folder
), by running following command in<venv_folder>
<condapath>\python.exe -m venv venv
- Now, your
<path_to_python_exe>
will be<venv_folder>\venv\Scripts.python.exe
. - If you need to install packages to this virtual environment, you use
<venv_folder>\venv\Scripts.python.exe -m pip install <package_name>
How to run Python file from command prompt by selecting anaconda or Python in windows?
First of all, you should update Python to at least 3.7.
On the first line of your script put this shebang:
#!/usr/bin/env python3
This is for Unix but I understand it works for Windows also. For more detail on shebangs, check here.
On Windows, the standard Python installer already associates the .py extension with a file type (Python.File) and gives that file type an open command that runs the interpreter (D:\Program Files\Python\python.exe "%1" %*). This is enough to make scripts executable from the command prompt as foo.py. If you’d rather be able to execute the script by simple typing foo with no extension you need to add .py to the PATHEXT environment variable.
For further reading: here
Related Topics
Check If Dataframe Has a Zero Element
Finding Non-Numeric Rows in Dataframe in Pandas
What Is the Correct Way to Make My Pyqt Application Quit When Killed from the Console (Ctrl-C)
Pyside - Pyqt:How to Make Set Qtablewidget Column Width as Proportion of the Available Space
How to Format an Integer to a Two Digit Hex
Find Matching Rows in 2 Dimensional Numpy Array
What Else Do I Need for Codehs 8.3.8: Word Ladder
Python Converting MySQL Query Result to Json
Python - How to Sort Multidimensional List to Two-Dimensional List
Python - How to Extract Elements from an Array Based on an Array of Indices
Regex to Append Some Characters in a Certain Position
Python | Count Number of False Statements in 3 Rows
Index All *Except* One Item in Python
Subtract One Dataframe from Another Excluding the First Column Pandas
How to Split Vector into Columns - Using Pyspark
Python: Create 50 Objects Using a for Loop
Python Opencv Cv2 - How to Increase the Brightness and Contrast of an Image by 100%