简体   繁体   中英

Anaconda Python module importing issues when running Python script from batch file

I want my Windows computer to automatically run a Python script every day, using the Task Scheduler. I wrote my Python Script using Spyder (Anaconda), and then I wrote a small batch file which looks like this:

set PATH="C:\ProgramData\Anaconda3\lib\site-packages";%PATH%
"C:\ProgramData\Anaconda3\python.exe" "path\to\my\python\script.py"
pause

note that I am manually adding "C:\\ProgramData\\Anaconda3\\lib\\site-packages" to my PATH variable, to ensure that my Anaconda Python distribution will correctly import the necessary modules, including pandas and numpy .

But when I run this batch script, the following error happens:

Traceback (most recent call last):
  File "path\to\my\python\script.py", line 10, in <module>
    import pandas as pd
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']

So it looks like Python wasn't able to import numpy , despite my specific action to add the site-packages folder to the PATH variable.

How can I solve this issue?

EDIT: my question is quite similar to this one Schedule a Python script via batch on windows (using Anaconda)

I finally solved it by writing the following:

call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
python "path\to\my\python\script.py"

The first command enables the Anaconda environment, ensuring that all the installed packages will be correctly imported when requested. Then the Python script is executed.

source https://stackoverflow.com/a/53363567/6103050

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM