开发者

How do you switch between python 2 and 3, and vice versa?

I am reading How To Learn 开发者_运维技巧Python The Hard Way, which uses 2. Recently discovered Invent With Python, which uses 3.

Can I download python 3, and use it when I read Invent With Python, then switch back to python 2 when I want to read How To Learn Python The Hard Way. If so, how would I choose which version I use?


Using 'virtualenv' you can have different isolated Python environments on a single machine. Also you can switch any-time between the different python interpreter versions.

What is virtualenv?

A Virtual Environment is an isolated working copy of Python which allows you to work on a specific project without worry of affecting other projects. It enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated.

How to install?

pip install virtualenv

To create virtual environment for python 2.7 :

root:~# which python2.7

/usr/bin/python2.7

root:~# which python3.4

/usr/local/bin/python3.4

You can also use a Python interpreter of your choice:

root:~# virtualenv -p /usr/bin/python2.7 Vpy27

Running virtualenv with interpreter /usr/bin/python2.7

New python executable in /root/Vpy27/bin/python2.7

Also creating executable in /root/Vpy27/bin/python

Installing setuptools, pip, wheel...done.

To begin using the virtual environment, it needs to be activated:

root:~# source Vpy27/bin/activate

The name of the current virtual environment will now appear on the left of the prompt:

(Vpy27) root:~# python -V
Python 2.7.3

Install packages as usual, for example:

(Vpy27) root:~# pip install junos-eznc    >> All pip installs done here, will be available only in this environment.

If you are done working in the virtual environment for the moment, you can deactivate it:

(Vpy27) root:~# deactivate   

To create virtual environment for python 3.4:

root:~# which python3.4

/usr/local/bin/python3.4

root:~# virtualenv -p /usr/local/bin/python3.4 Vpy34

root:~# source Vpy34/bin/activate

(Vpy34) root:~# python -V
Python 3.4.4

There is also a way to create virtual environment with already available site-packages.


depends on your system/platform...

I'm currently on Ubuntu 10.10 and have both 2.6 and 3.1 installed. The default system python is 2.6, and python3 is installed as an additional package.

corey@studio17:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
corey@studio17:~$ python3
Python 3.1.2 (release31-maint, Sep 17 2010, 20:27:33) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

similarly, on Windows, I have 2.6 and 3.1 installed (in C:\Python26 and C:\Python31)

easy to switch back and forth.


also.. there are some syntactic differences between 2.x and 3.x that you will need to be aware of (print is a function, etc).


In windows 10 it is a lot easier then what have been given by users above.

Install both the version in separate folders, and then go to environment variable and add the path for both the versions.

Now any time you want to run particular version, just change its order (path) and move it to top of other version, and then restart the cmd and type python this time, you will see that only that particular version of python will run.

How do you switch between python 2 and 3, and vice versa?

For example in my case, I have two version of python one in anaconda(v3.0.6) and another is python 2.7

anytime I want to run the 2.7 i move its path above the anaconda version, as you can see in the screenshot above, and move it below when i want to run anaconda version.


I'm on Windows 10 with Python 3.5 and 2.7. Using PowerShell, here's how I'm switching between versions.

############################################################
# Switch to Python 2.7
############################################################

# Remove python 3.5 from PATH
$current_path = [Environment]::GetEnvironmentVariable("Path", "User")
$python3_path = "C:\Users\REPLACEUSER\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\REPLACEUSER\AppData\Local\Programs\Python\Python35-32\;"
$new_path = $current_path.replace($python3_path, "")
[Environment]::SetEnvironmentVariable("Path", $new_path, "User")

# Add python 2.7 to PATH
# Run PowerShell as administrator
$current_path = [Environment]::GetEnvironmentVariable("Path", "Machine")
$python2_path = "C:\Python27\;C:\Python27\Scripts;"
$new_path = $python2_path + $current_path
[Environment]::SetEnvironmentVariable("Path", $new_path, "Machine")

# Restart PowerShell to see change

# Confirm change
python --version


############################################################
# Switch to Python 3.5
############################################################

# Remove python 2.7 from PATH
# Run PowerShell as administrator
$current_path = [Environment]::GetEnvironmentVariable("Path", "Machine")
$python2_path = "C:\Python27\;C:\Python27\Scripts;"
$new_path = $current_path.replace($python2_path, "")
[Environment]::SetEnvironmentVariable("Path", $new_path, "Machine")

# Add python 3.5 to PATH
$current_path = [Environment]::GetEnvironmentVariable("Path", "User")
$python3_path = "C:\Users\REPLACEUSER\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\REPLACEUSER\AppData\Local\Programs\Python\Python35-32\;"
$new_path = $python3_path + $current_path
[Environment]::SetEnvironmentVariable("Path", $new_path, "User")

# Restart PowerShell to see change

# Confirm change
python --version

Note that you will need to update paths to reflect your Python versions and user profile.


A couple ways on *nix systems:

  • Install into separate directories (e.g. /usr/local/python2 and /usr/local/python3) and create a link (e.g. /usr/bin/python) which you change to point to whichever executable you want to use.
  • Same install as above, but set up separate python commands (e.g. /usr/bin/python2 and /usr/bin/python3) and call those when you want to invoke python. Or have the python command default to one of those and a pythonN for the other (N = 2 or 3, whichever isn't the default).


Yes you can. On my machine at least(Vista), v2 and v3 have completely separate idles allowing me to run whichever version I feel like when I feel like it. So go ahead and download v3.


if you are using windows 10 and have python 2.x and 3.x.

  1. open control panel > system and security > system
  2. click advanced system settings.
  3. click environment variables.
  4. click path and edit and then make the path of python version you want to use above that you don't want to use [by click the moveu Up button]
  5. restart powershell.
  6. python --version


Here are my 2 cents.

If you are on a unix based system (Ubuntu, etc..), and you currently have python 2.x. Go ahead and download the python 3.x from Python.org

After installation it will create a separate directory python3

You are done.

To run your programs with python2.x use python filename.py

To run your programs with python3.x, use python3 filename.py

Similarly, to fire up the python2.x and python 3.x interpreter use python and python3 respectively.

I see some of the answers pointing you to virtualenv, I don't think that is what you were asking for, virtualenv is used for isolating Python environments.


If you are using anaconda:

Create a Python 2 environment named py2, install Python 2.7:

conda create --name py2 python=2.7

Activate and use the Python 2 environment:

WINDOWS:

activate py2

LINUX, macOS:

source activate py2

Deactivate the Python 2 environment:

WINDOWS:

deactivate

macOS, LINUX:

source deactivate

Similarly for py3

Create a Python 3 environment named py3, install Python 3.5:

conda create --name py3 python=3.5

and so on..


On Windows, the Python launcher can do this for you.

The PEP article says:

Shebang line parsing

If the first command-line argument does not start with a dash ('-') character, an attempt will be made to open that argument as a file and parsed for a shebang line according to the rules in [1]::

#! interpreter [optional-arg]

So simply add a shebang at the beginning of your Python script, like this:

#! python3
#coding=utf-8

import sys
print(sys.version)
...

Or you can pass a command-line parameter to the py.exe launcher:

C:\Users\Administrator>py -3 my_script.py

C:\Users\Administrator>py -2 my_script.py


I've tried 6 solutions so far, like:

virtualenv --python=python py27env
mkvirtualenv --python=python3 py3env etc..

also using virtualenvwrapper package etc. None of them worked.

I have Python 3.6 and Python2.7 on my Windows 10 machine, so I renamed C:\Python27\python.exe to python2.exe and C:\Python36\python.exe to python3.exe or you can even use python36.exe format. Of course C:\Python27, C:\Python27\Scripts, C:\Python36, C:\Python36\Scripts added to Environment Variables PATH.

(1) For python3 just type:

python3 -m virtualenv venv3

(2) Go to venv folder, activate it with:

Scripts\activate.bat

(3) (venv3) shows it's activated:

(venv3) HOME1@PC C:\Builts\venv3

(4) and then check if it is really 3.6:

python --version

Python 3.6.0

For python2:

python2 -m virtualenv venv2

Result:

(venv2) HOME1@PC C:\Builts\venv2
python --version
Python 2.7.9

I hope it will help.


I just think that there is no need to set up the environment to switch from python2 to python3. Instead when compiling the python script file in the command line, instead of typing python you type python3 then python3 will be used.

# python3 myscript.py


To be possible install virtualenv for python2 without admin access. Rename python.exe to python27.exe at python2 folder; Include python27 and python27\Scripts at Path environment variable for your account. call prompt cmd

python27 <path to Scripts\pip.exe of python2.7> install virtualenv

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜