Dealing with Multiple Python Versions— do not run independently

后端 未结 5 1309
无人共我
无人共我 2021-01-14 03:09

I have Python 2.7 (my preferred version) and 3.3 (version used in my Programming class) installed on my computer (OS: Windows 7). Until a certain point, they worked fine ind

相关标签:
5条回答
  • 2021-01-14 03:45

    You can invoke the python interpreter in Python3.3 by running py filename.py while if you run them in Python 2.7 you can just run python filename.py.

    On my windows box I have small batch scripts (called python2 and python3) which call the relevant python source.

    filename:    python3.bat
    code:        C:\Python32\python.exe %*
    
    0 讨论(0)
  • 2021-01-14 03:47

    You should focus on the Windows specific features here. The #! way was usefull only for Unix based operating systems until recently.

    Python 3.3 introduced Python Launcher for Windows for exactly that purpose. When Python 3.3 was installed on your machine, the installer copied py.exe and pyw.exe launchers into c:\Windows, and the .py and .pyw extensions were associated with the launchers.

    This way, if the .py file is not launched explicitly via Python interpreter, the Python Launcher is used. You can also explicitly launch py script.py.

    If there is no information about Python version, the highest Python 2.x is used. There are two ways how to tell the launcher that you want to launch Python 3: 1) explicitly py -3 script.py, 2) write #!python3 to the first line inside the script.py.

    The wanted Python version can be more detailed (to execute the specific version of Python). See the mentioned doc. I have also summarized my surprise with that situation in the article Do you know the "Python Launcher for Windows"? at Experts Exchange.

    0 讨论(0)
  • 2021-01-14 03:47

    You can launch with a specific version of python from the command line by using 'py' followed by a version flag (i.e. -2 or -3)

    Here is an example of what you could do:

    Python 2

    >>> py -2 your_script.py
    

    OR

    Python 3

    >>> py -3 your_script.py
    
    0 讨论(0)
  • 2021-01-14 03:55

    The Python documentation for python 3.2 provides information which helped me to call different python 2.7 and 3.3 separately.

    http://docs.python.org/3/using/windows.html#customizing-default-python-versions

    0 讨论(0)
  • 2021-01-14 04:03

    You could use shebang lines:

    #! /usr/bin/env python2
    

    for Python 2.x scripts and:

    #! /usr/bin/env python3
    

    for Python 3.x scripts. You can use more specific versions e.g., python3.3

    You can configure default Python version.

    0 讨论(0)
提交回复
热议问题