"ImportError: DLL load failed while importing _ssl" when launching from batch file

5.8k Views Asked by At

I want to send a mail via python script and launch the script with a batch file. I get the error "ImportError: DLL load failed while importing _ssl" when launching from the batch file, when running the python script directly from my spyder editor it works as intended.

I am using a Windows 10 and a freshly installed anaconda environment with python 3.8, which is installed into "C:\Users\Max".

My batch file looks like this:

"C:\Users\Max\anaconda3\python.exe" "C:\Users\Max\Documents\Python Scripts\script.py" 

My Python Skript looks like this:

import smtplib, ssl
port = 465  
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port) as server: 
   server.login("[email protected]", "password")
   server.sendmail("[email protected]", "[email protected]", "message")

Why does the script behave differently when called from a batch file and how do I fix this?

1

There are 1 best solutions below

1
Max Mustermann On BEST ANSWER

Ok I got it!

I first had to put "C:\Users\Max\anaconda3\condabin" in my PATH in order to use conda commands in my windows cmd, then modify my batch file to

call activate base
"C:\Users\Max\anaconda3\python.exe" "C:\Users\Max\Documents\Python Scripts\script.py" 
call conda deactivate