0

I'm using Anaconda. I created an environment called ENGINEERING. In than environment I installed python 3.6, pandas 1.1.3, spyder 3.3.6, numpy 1.19.2, and many more. The base environment has these packages also but not necessarily the same version. Within the ENGINEERING env I created a python script in Spyder that runs without any issues when I run it in spyder. Then, I want to automate that script by creating a .bat file and then using that to automate via windows task scheduler. My .bat looks like this:

"C:\Users\alopo000\Anaconda3\envs\engineering\python.exe" "C:\Users\alopo000\Files\Python\Valid\Portf.py"
pause

when I run the .bat I get an error message:

ImportError: Unable to import required dependencies:
numpy:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.6 from "C:\Users\alopo000\Anaconda3\envs\engineering\python.exe"
  * The NumPy version is: "1.19.2"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed: The specified module could not be found.

I've tried many things to fix this. I went to the website they suggested and couldn't find anything there. I uninstalled pandas and numpy and reinstall them again but still, same error. The one thing I haven't tried is to fix the VARIABLE PATHS, mostly because I've read in many places not to change those. I can change them if that's what it is, but I would like to understand first why it is not recommended. I read it can create issues with other versions of python but not sure if that's what it is.

Do I need to activate the environment in the .bat file? is the problem that the .bat file is trying to run it outside the environment? Any help would be appreciated.

chulo
  • 53
  • 8
  • You have to activate the venv, yes. - https://stackoverflow.com/questions/34622514/run-a-python-script-in-virtual-environment-from-windows-task-scheduler See the accepted answer - it uses && to activate the env and then run the script. :) – h4z3 Jun 14 '21 at 16:30
  • You should `call activate.bat`! – Compo Jun 14 '21 at 16:59
  • @h4z3 I tried that but didn't work. I actually went to the scripts folder and didn't see any activate.bat file there. – chulo Jun 14 '21 at 18:30
  • I do see the activate.bat file in the base environment though – chulo Jun 14 '21 at 20:11
  • You need to find the correct venv you used for this script and activate it. I have no idea where you have it. Just find what you activate during development and activate the same one. :) – h4z3 Jun 14 '21 at 23:03

1 Answers1

1

You will need to upgrade/change your base Python & numpy version installations to match those specified (3.6, 1.19.2). I had the same issue and same situation as OP (write/dev program in a virtual Spyder environment "spyder-env", then automate .py file with WTS). I tried copying over & running the 'activate.bat' in the /scripts/ folder of the virtual environment but that didn't work. In my situation, my conda spyder-env was Python = 3.9.12 and numpy = 1.21.5.

I ended up upgrading my base global Python version (conda install) for Python which changed from 3.8.3 and did the same with numpy (had to downgrade). I'm not sure if this is exactly necessary, why this works, or if it may cause problems down the line (I was about to just re-install conda).

The appropriate .bat file should be something like this once you do that:

call "C:\Users\alopo000\Anaconda3\Scripts\activate.bat"
call "C:\Users\alopo000\Files\Python\Valid\Portf.py" %*
REM Uncomment below to debug
REM pause
1337nerd
  • 33
  • 6