0

I have installed Anaconda with Python3. Then, I additionally created a virtual environment with Python2. There are no other Pythons on the computer. My problem:

If I run the command

python C:\Path\To\myScript.py arg1 arg2

in CMD, Python 3.4 is used to execute myScript.py (as expected/desired). But! If I create a .bat file that contains precisely the upper command, Python 2.7 is used. (I check the version with the command print(sys.version) in myScript.py).

How can I fix that?

4
  • check your path variable Commented May 26, 2016 at 7:52
  • ...;C:\Anaconda3;C:\Anaconda3\Scripts;... are the only two Python-related parts. In C:\Anaconda3, there is python.exe that starts Python3. In C:\Anaconda3\Scripts there is no python.exe. Commented May 26, 2016 at 8:01
  • try appending the path variable with the path of your python installation folder. And then try it again Commented May 26, 2016 at 8:04
  • @HassanMehmood I thought C:\Anaconda3 is my python installation folder. Commented May 26, 2016 at 8:13

3 Answers 3

2

Try specifying the full path to the Python3 (ie. /path/to/Python3 ) executable in your batch script. It's probably defaulting to the system python.

If you are using a virtualenv, and you probably should be, there is a separate python executable at venv/bin/python (or similar under Windows) - using this specific executable by absolute path is often the easiest way to ensure the correct python environment is being used, especially when scripts are run automatically or by a different user. This is entirely by design, virtualenv is often used this way.

Sign up to request clarification or add additional context in comments.

7 Comments

This works (and is currently the only solution), but does not answer the question in a clean way:)
I'm not sure what criteria you are using for "clean" but I would offer that this is a typical solution for things like init.d scripts that need to use a virtualenv, and be run at boot on linux systems. If you have additional feedback, on what would make it cleaner, I may be able to offer something more. I can also say that, at least on posix systems, changing which python used by the system is never a good idea.
Well, a clean way to solve this issue would be to i) find an explanation, why can the same command mean two different things, and ii) possibly eliminate the cause of the issue. (i.e. instead of taking pain killers and banging my head against the wall, I would rather find out that i) banging the head against the wall causes the pain, and ii) stop banging my head against the wall).
I realize you are on windows, but on Posix, the "why" would be that the user running it is different. That user has a different environment, and hence python3 is ambiguous. a batch file is run by the system, and hence system python is assumed. you want different behavior, so you need to specify what behavior you want. By indicating the full path. There could be a lot of different python3 executables on a given box - but only one in each folder.
I have given you a working solution. I would humbly suggest that this answer leads to the cause, is not a symptom, and acceptance of it will reduce your reliance on pain killers, and allow you to stop banging your head at the same time. If I were you, I'd hit save on my working batch script, answer on this SO answer, and happily have a beer while I worked out the "why?" for my platform of choice.
|
0

since you're using Anaconda, adding a line source deactivate before the python command would deactivate any virtualenv explicitly

edit: it's probably just deactivate for windows cmd prompt

3 Comments

This does not help: deactivate in .bat causes an error probably (the window disappears, where as source deactivate has no influence on the following python ... command.
strange, it does not close cmd for me. Quick fix for now is specifying full path
source deactivate causes an error: source is not recognized as an internal or external command, to be precise. I suppose that specifying the whole path is the best for now, indeed.
0

You can either change the default version of python by changing the value of path variable this can be done by following this answer How to update PATH variable permanently from cmd? Windows

Or you can temporarily change the version you want to use follow this for that How to run different python versions in cmd

I hope my answer was helpful.

1 Comment

Not really, I am afraid (see my comment under the question) :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.