How to Use Anaconda Python to Execute a .Py File

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

  1. Create a .bat file (e.g. run_python_script.bat) with contents shown below.
  2. Create task in "Task Scheduler" to run the .bat file.

1.a. The .bat file contents with conda environments

  1. Check your <condapath>. Your conda.exe is located at <condapath>/Scripts.
  2. 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 the python.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

  1. Go to "Task Scheduler" -> "Create Basic Task"
  2. Give the name & timing info
  3. 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

  1. Check your <condapath>. Your conda.exe is located at <condapath>/Scripts.
  2. 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

  1. Now, your <path_to_python_exe> will be <venv_folder>\venv\Scripts.python.exe.
  2. 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



Leave a reply



Submit