0

I would like to auto run a Python script.

First, I created a *.PY file and ran it in Anaconda Prompt within my Environment successfully.

Next, I created a .BAT file where the Python PATH was Python within the Environment I wish to run, and then the .PY file. I created the file in Notepad and saved it with extension .BAT:

C:\Users\fh\Anaconda3\envs\myenvo\python.exe "C:\Users\fh\Documents\Python Scripts\myautocode.py"
pause

Now, when I run the .BAT file from Windows Explorer via double click, I get the following error in a command prompt window:

File "C:\Users\fh\Anaconda3\envs\myenvo\lib\ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ImportError: DLL load failed while importing _ssl: The specified module could not be found.
Press any key to continue . . .

Once I can get this to work, I plan to run it automatically using Windows Task Scheduler. I'm following the process as described here:

https://towardsdatascience.com/automate-your-python-scripts-with-task-scheduler-661d0a40b279

**I realized my issue was really I wasn't calling my environment correctly - so the real question was, how should you load the environment in your BAT file, I will answer the question below. This is how I should have had my BAT file **

call C:\Users\fh\Anaconda3\Scripts\activate.bat 
call conda activate myenvo
python.exe "C:\Users\fh\Documents\Python Scripts\project\run_project_auto.py"
pause

Thank you

fazistinho_
  • 195
  • 1
  • 11
  • 2
    This is not in any way specific to being started from a .bat file. (It may be specific to a difference between the environment your .bat file runs in, and the environment you test your Python script in). – Charles Duffy Nov 16 '20 at 17:54
  • @CharlesDuffy I thought of that. I searched for `Python.Exe` on my PC and found two instances within subfolders (`C:\Users\fh\Anaconda3\envs\myenvo\` and `C:\Users\fh\myenvo\Scrips\`), I tried using paths to each in my `.BAT` file, one has the error above, the other had an error saying Numpy missing so I know that's not the one. Is that the correct way to access Python within my Environment via a `.BAT` file? Or is there another way to ensure I'm using the same environment. – fazistinho_ Nov 16 '20 at 18:13
  • 1
    I'd suggest dumping the `PATH` variable in both the environment where your script works, and the environment where it doesn't, and comparing the two. Maybe `PYTHONPATH` as well, though it's more likely to be `PATH` that's the culprit. – Charles Duffy Nov 16 '20 at 18:20
  • 1
    ...maybe put a line like `print(repr(os.environ["PATH"]), file=__import__("sys").stderr)` before the code that tries to import a library that needs `_ssl` in your script, and compare between the working and non-working cases. – Charles Duffy Nov 16 '20 at 18:21
  • Ok, @CharlesDuffy I know what the problem is, I am not activating the environment properly in my .BAT file (simply starting python.exe from the env folder doesn't do the job it seems). I found one resource on it: https://stackoverflow.com/questions/56026422/how-to-start-python-virtual-environment-in-bat-batch-file but it doesn't work for me, do you have any other suggestions? – fazistinho_ Nov 16 '20 at 18:53
  • 1
    I'd need a reproducer and a description of exactly _how_ it "doesn't work". Probably best to post a new question showing the details of how you're trying to activate the virtualenv and exactly what happens in response (whether the activate attempt itself fails, or the PATH isn't updated, or whatever else is going on). – Charles Duffy Nov 16 '20 at 18:57

0 Answers0