Using multiple versions of Python

前端 未结 7 624
温柔的废话
温柔的废话 2020-12-06 10:25

I have both Python 3.3 and Python 2.7 installed on my computer. The python 3.3 works fine, but when I try to run something using python 2.7, it is still referencing python

相关标签:
7条回答
  • 2020-12-06 10:39

    Google search results have returned a few useful resources that answer your problem.

    Python Docs

    The Python Documentation (http://docs.python.org/3.3/using/windows.html#python-launcher-for-windows) gives a quick overview for running multiple versions on the same machine.

    The first option would be to include your python version in the file you wish to execute using something along the lines of

    #! python
    Your code here

    To execute in Python 2, or

    #! python3
    Your code here

    For running the code in your Python 3 version. Then you would simply use "python yourscript.py" and the python version would be specified by the Python script.

    StackExchange Sites

    There are multiple other questions which may address the problem you are facing:
    How to install both Python 2.x and Python 3.x in Windows 7

    Or for Ubuntu 13: Ubuntu 13.04 Install and running Python 3 at the same time than Python 2.7.x

    Or for Mac using Homebrew: How can I use Homebrew to install both Python 2 and 3 on Mac?
    And a video reference for Mac without Homebrew: http://www.youtube.com/watch?v=c9LlK2iu7OA

    0 讨论(0)
  • 2020-12-06 10:39

    You will get issue if multipler version of python is set

    File "F:\PYTHON33\LIB\site.py", line 173 file=sys.stderr) ^ SyntaxError: invalid syntax

    To fix this issue remove previous version python set in system environment variable

    0 讨论(0)
  • 2020-12-06 10:42

    If you want to use different versions of Python try something like VirtualEnv.

    UPDATE: Additional topic for you: Use different Python version with virtualenv

    0 讨论(0)
  • 2020-12-06 10:46

    Changing the executable files isn't an option for everyone, and uninstalling can risk breaking programs that depend on that Python installation.

    My answer here doesn't guarantee that you can run both versions side by side seamlessly, but I was able to resolve it without something as major as uninstalling.

    In my case, the problem was that the PYTHONPATH and PYTHONHOME environment variables were both set to the 3.x installation path, whereas I needed to use the 2.x installation. Replacing these environment variables with the 2.x path and restarting the shell was an acceptable workaround for me.

    0 讨论(0)
  • 2020-12-06 10:48

    Setting the PYTHONPATH environment variable to nothing fixed this issue for me.

    0 讨论(0)
  • 2020-12-06 10:52

    I received the same error when I was running my python file using python filename.py my PYTHONPATH was set to use python3

    I fixed it by using the below command to run my file

    python3 filename.py
    
    0 讨论(0)
提交回复
热议问题