I am trying to run a python script via a batch file (eventually to be run in task scheduler). When I run the file in PyCharm it works correctly. However, when I try to run it with the batch file it seems to be using a different version of Python (I am getting a numpy import error). I am using Windows 10 and the file is inside of an Anaconda project. I have checked that the Anaconda version of Python is first in line in the PATH. Not sure where to go from here.
-
Find out the python environment associated with Pycharm. Take look here: jetbrains.com/help/pycharm/configuring-python-interpreter.htmlLutfar Rahman Milu– Lutfar Rahman Milu2020-07-04 17:46:58 +00:00Commented Jul 4, 2020 at 17:46
-
I did this and linked PyCharm to the Anaconda version. The file runs correctly through PyCharm but not when I run the batch file.chc5286– chc52862020-07-04 18:18:25 +00:00Commented Jul 4, 2020 at 18:18
-
This might be helpful: stackoverflow.com/questions/20081338/…Lutfar Rahman Milu– Lutfar Rahman Milu2020-07-04 19:25:45 +00:00Commented Jul 4, 2020 at 19:25
-
Sort of related (note how to execute an arbitrary Python script within a specific environment): stackoverflow.com/a/58458618/570918merv– merv2020-07-06 07:17:10 +00:00Commented Jul 6, 2020 at 7:17
1 Answer
Your script is probably running in a virtual environment. In practice that means that some environment variables change, so python will run a specific binary, and packages will be looked for at some specific place. But well, you know that, as you mention the PATH thing.
To find out the virtual environment you can get and print your environment variables from your script. When run from PyCharm you should see the change in the PATH you mention, but also some other PY-related variables pointing inside your Anaconda directories.
Still better, you might be able to find a .../scripts/activate.bat or .../bin/activate (Windows or Linux, respectively). Calling that at the beginning of your script should do the trick.