1

I try to save a python script as a shortcut that I want to run. It opens it but then closes right away.

I know why it is doing this, it is opening my windows command line in python3.2 the script is in python 2.7

I need both version on my PC, my question is how to I change the cmd default.

I have tried to "open with" shortcut on the icon and it just continues to default to 3.2.

Help please

2
  • what code does that file contain? Commented Dec 4, 2012 at 20:57
  • its just a simple python print"hi my name is" code with raw_input in it. I know this would error out in 3.2 Commented Dec 4, 2012 at 21:00

2 Answers 2

3

To change the "default", just edit your PATH environment variable (My Computer > Properties > Advanced > Environment Variables) to include only whichever Python installation you want as the default (e.g. C:\Python32\).

To have both quickly available, I recommend you do something like this:

Create a directory somewhere on your machine where you'll remember it (mine's C:\users\me\scripts). Put this directory at the front of your PATH environment variable. Now make two batch scripts in this directory; call them Python27.bat and Python32.bat. They should look like this (example for 2.7):

@ECHO OFF setlocal set PYTHONHOME=C:\Python27 set PYTHONPATH=%PYTHONHOME%\lib;%PYTHONPATH% %PYTHONHOME%\python.exe %* endlocal 

This script sets the PYTHONHOME and PYTHONPATH variables (locally, just for this session of Python) to be whatever is appropriate for each particular installation. Then it launches the correct version of Python (and passes any other arguments to it that you might have specified). So, to start the correct version of Python, type Python27 or Python32 at your command prompt and you'll launch the appropriate Python, with the correct environment already in place. This works for launching programs, too:

# test\foo.py import sys print('version is %s' % sys.version) 

C:\>Python27 test\foo.py version is 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] C:\>python33 test\foo.py version is 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] 

You can also take a look at virtualenv, which is a very powerful tool for setting up side-by-side Python environments. It certainly has some good uses, but personally I find it cumbersome to use for simple things like you're doing here.


Finally, if you want to be able to double-click it, that's a whole different set of issues. You can change filetype associations as indicated here (Windows 7 here), but I don't know of a simple way to make it modular like the command-line scripts above.

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

Comments

0

Install both pythons, and change the path in Windows, by default both Pythons will be PATH=c:\python\python 2.7 and PATH=c:\python\python 3.2 Or something like that. What and since windows stops as soon as it finds the first python, what you could do is have one called PATH=c:\python27\ and another PATH=c:\python32\ this way you can run both of them.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.