chc5286
chc5286

Reputation: 45

How to change default python environment when running a .py file in windows batch file

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.

Upvotes: 0

Views: 538

Answers (1)

olepinto
olepinto

Reputation: 399

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.

Upvotes: 2

Related Questions