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?

Antoine
  • 664
  • 6
  • 19

3 Answers3

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.

domoarigato
  • 2,174
  • 3
  • 20
  • 33
  • This works (and is currently the only solution), but does not answer the question in a _clean_ way:) – Antoine May 26 '16 at 08:09
  • 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. – domoarigato May 26 '16 at 09:10
  • 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). – Antoine May 26 '16 at 13:06
  • 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. – domoarigato May 26 '16 at 21:06
  • 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. – domoarigato May 26 '16 at 21:07
  • The problem is that I think I am not using any virtual env. at that moment. As far as I understand (I might be wrong), I have virt. env. only for Python2 and no part of the `PATH` variable leads to it: `C:\Anaconda3` contains precisely one `python.exe` and this is for Python3 (I have checked by running it). In `C:\Anaconda3\Scripts` there is no `python.` file. So, the question is, i) how can Python2 be a default for the system (how can the system find any python2.exe or something), and ii) why `python` in .bat has different meaning as in CMD? (since the same user is behind both). – Antoine May 27 '16 at 11:58
  • You are able to specify the full path of the correct python3 executable, and make the script work, right? If you intend to use this script with python3, you dont need a python 2 virtualenv - why not just delete it? I cant see what purpose the venv serves. I don't understand why you care so much about specifying the desired Python3 executable through the environment variable, vs an absolute path. – domoarigato May 27 '16 at 12:05
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

Ayush
  • 3,021
  • 28
  • 39
  • 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. – Antoine May 26 '16 at 07:56
  • strange, it does not close cmd for me. Quick fix for now is specifying full path – Ayush May 26 '16 at 07:59
  • `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. – Antoine May 26 '16 at 08:07
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.

Community
  • 1
  • 1
Ronak Khandelwal
  • 281
  • 1
  • 14