1

Running Windows 7. 2.7, 3.3 and 3.4 installed.

I just installed Python 3.3 for a recent project. In the command prompt, python launches 3.4, and py launches 3.3. I can access 3.3 using the 3.3 version of IDLE, but how can I access it via the command prompt? Is there a shortcut like py that I can use? Do I need to define this on my own like an alias? Or is the best route to somehow change the path to temporarily make 3.3 the default?

Just downloaded virtualenv, maybe that might be part of the solution.

AllTradesJack
  • 2,762
  • 5
  • 25
  • 36

2 Answers2

2

If you want to male a file specifically open with a version you can start the file with #! python3.x the x being the version you want. If you want to be able to right click and edit with that version youll need to do some tweaking in the registry

James Kent
  • 5,763
  • 26
  • 50
  • That's a useful tip, so whenever you execute `filename.py`, the command prompt will read that shebang to decide how to run it? Also, what is meant by "tweaking in the registry"? – AllTradesJack Jul 17 '14 at 22:48
  • 1
    Yeah, as long as you dont put python before the filename in the path then the file gets opened by the py.exe or pyw.exe in the windows folder, which decides which version to run it with, in the absence of that line (called a shebang) it gets passed to the oldest version (my experience) and if you open the registry and open hkey classes you will find an entry for python.file and another for python.noconfile , these each have subkeys for the shell containing another key for editing with idle and the command to do that with, by adding new keys you can create your own options to use that version – James Kent Jul 17 '14 at 22:53
  • You can also explicitly execute `py -3.3 filename.py` from a command line. – pepr Jul 18 '14 at 10:17
1

To make it so you can run the different versions of Python from the command prompt, go into C:\Python33 and C:\Python34 and make copies of the python.exe that resides there. Rename the copy to python33.exe and python34.exe, respectively, then you can just run

python33 file.py

or

python34 file.py

from the command prompt, and you'll be assured the correct version will be used. I'd also make a copy of the python.exe in C:\Python27 and name it python2.exe, just so you can specify that version when needed, if you don't remember the search order of your Python directories in your PATH environment variable.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • I copied python.exe, pasted the copy in that same `python33` directory, and renamed the new copy `python33.exe`. But when I execute `python33` in the command prompt I get: `'python33' is not recognized as an internal or external command, operable program or batch file.` – AllTradesJack Jul 17 '14 at 23:00
  • 1
    @joshsvoss make sure you restart the command prompt, and ensure no spelling errors have snuck in anywhere. Also, are you sure that `C:\Python33` is in your `PATH`? Type `echo %PATH%` to find out. – MattDMo Jul 17 '14 at 23:02
  • You're right, I just added `C:\Python33;` to the path, as well as `C:\Python33\Scripts;`, since that both 2.7 and 3.4 had the `\Scripts` folder in the path as well. Any reason to leave the originally named `python.exe` there? And could it ever cause namespace problems with the other two `python.exe` files? – AllTradesJack Jul 17 '14 at 23:15
  • 1
    @joshsvoss Yes, you'll want to leave them there as other programs may depend on them. You won't get any namespace issues if you just run `python` from the command prompt, it'll just run the first `python.exe` it sees in your `PATH`. So, if you want a default, put that directory first in the `PATH`. – MattDMo Jul 17 '14 at 23:21
  • This approach was obsoleted by Python 3.3+ that introduced the Python launcher for Windows (see http://legacy.python.org/dev/peps/pep-0397/). The Python versions should not be added to the path. Instead of `python`, the `py` should be called if you want to do that explicitly (call `py -3.3 myscript.py` instead of `python33 myscript.py` if you want the specific version). Or just call `py myscript.py` and put the `#!python3.3` at the first line of the script. – pepr Jul 18 '14 at 10:22
  • @pepr - I wasn't aware of `py` and its functionality, thanks for that. I was just playing with it on my work laptop (Win8.1) and there are a couple potential issues for certain users. In my case, security settings preclude me from running many installers, as they require elevated access to change the registry, install to `Program Files`, modify the `C:\Windows` hierarchy by installing shared libs, etc. On my laptop, I got IT to install Python 2.7 and 3.3, but when 3.4 came out I didn't feel like going through the hassle, so I just manually extracted the `.msi` file and put the contents in ... – MattDMo Jul 18 '14 at 16:29
  • @pepr ... the `C:\Python34` directory (which I was somehow allowed to make :) So, 3.4 isn't "installed" on my system, as there's no registry entries for it (even though I was able to modify my personal `PATH` env. var to point to it), so `py.exe` is unable to find 3.4, whilst 2.7 and 3.3 work fine. I much prefer working in Unix/Linux to Windows, so I'm used to having `python`, `python3`, `python2.6`, `python2.7`, `python3.3`, `python3.4`, etc. binaries laying around, and I can create symlinks in my personal `~/bin` directory for whichever version(s) I want to use. That's why I recommended ... – MattDMo Jul 18 '14 at 16:34
  • @pepr ... the solution I did for the OP. I will keep `py.exe` in mind for the future, however. – MattDMo Jul 18 '14 at 16:34
  • @MattDMo: No problem. Your approach is fine as long as you want to manage it this way. In Unix, `#!` and `chmod +x` is a nice example on how it should be done. The `py.exe` is just to make it a bit similar also in Windows. I personally work with administrator permissions on Windows. The extra level of elevated permissions is the way to make big mistakes. I do not want to judge, I just use it ;) – pepr Jul 18 '14 at 16:45