Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run different python versions in cmd [duplicate]

How can I configure windows command dialog to run different python versions in it? For example when I type python2 it runs python 2.7 and when I type python3 it runs python 3.3? I know how to configure environment variables for one version but two? I mean something like Linux terminal.

like image 725
hamidfzm Avatar asked Dec 26 '13 14:12

hamidfzm


People also ask

How do I run a different version of Python from the command line?

As a standard, it is recommended to use the python3 command or python3. 7 to select a specific version. The py.exe launcher will automatically select the most recent version of Python you've installed. You can also use commands like py -3.7 to select a particular version, or py --list to see which versions can be used.

What happens if I have 2 versions of Python?

There's nothing wrong with having two versions of python installed, and it's actually quite common to do so. Usually, one would install them with different names ( python vs python3 , for example) to avoid confusion though.

How do I run two versions of Python on Windows?

Install multiple python versions For Windows users, I recommend using the Windows x86-64 executable installer option if you work on a 64bit system. Otherwise, just use the Windows x86 executable installer . After locating the install option for the specific version, just press the download link.

How to choose a version of Python to run your scripts?

Follow the same steps for installing Python 2 and Python 3 versions. Now you have two Python version installed on your system. But, how to choose a particular Python version to run your Python code or script. Simply add the following line of code as the first line to your any Python script. You can print the Python version to ensure it.

How do I run a different copy of Python?

Running a different copy of Python is as easy as starting the correct executable. You mention that you've started a python instance, from the command line, by simply typing python.

How to install two versions of Python?

You can simply download Python software from its official website and install it. For more detail, choose below installation steps based on the operating system. Follow the same steps for installing Python 2 and Python 3 versions. Now you have two Python version installed on your system.

How do I launch a python script from the command line?

Then you can launch the script by simply typing the scriptname.py on the cmd line, od more explicitly by py scriptname.py, and also by double clicking on the scipt icon. The py.exe looks for C:\PythonXX\python.exe where XX is related to the installed versions of Python at the computer.


2 Answers

I also met the case to use both python2 and python3 on my Windows machine. Here's how i resolved it:

  1. download python2x and python3x, installed them.
  2. add C:\Python35;C:\Python35\Scripts;C:\Python27;C:\Python27\Scripts to environment variable PATH.
  3. Go to C:\Python35 to rename python.exe to python3.exe, also to C:\Python27, rename python.exe to python2.exe.
  4. restart your command window.
  5. type python2 scriptname.py, or python3 scriptname.py in command line to switch the version you like.
like image 199
Leung Ying Ying Avatar answered Sep 26 '22 07:09

Leung Ying Ying


Python 3.3 introduces Python Launcher for Windows that is installed into c:\Windows\ as py.exe and pyw.exe by the installer. The installer also creates associations with .py and .pyw. Then add #!python3 or #!python2 as the first lline. No need to add anything to the PATH environment variable.

Update: Just install Python 3.3 from the official python.org/download. It will add also the launcher. Then add the first line to your script that has the .py extension. Then you can launch the script by simply typing the scriptname.py on the cmd line, od more explicitly by py scriptname.py, and also by double clicking on the scipt icon.

The py.exe looks for C:\PythonXX\python.exe where XX is related to the installed versions of Python at the computer. Say, you have Python 2.7.6 installed into C:\Python27, and Python 3.3.3 installed into C:\Python33. The first line in the script will be used by the Python launcher to choose one of the installed versions. The default (i.e. without telling the version explicitly) is to use the highest version of Python 2 that is available on the computer.

like image 28
pepr Avatar answered Sep 26 '22 07:09

pepr