Search code examples
python-2.7windows-10python-3.5python-install

Cannot run python 3.5 along with python 2.7.4 in Windows 10


I am trying to install Python 3.5.0 alongside with Python 2.7.4.

C:\Users\Animesh>python
Python 2.7.4 (default, Apr  6 2013, 19:55:15) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\Animesh>py
Python 2.7.4 (default, Apr  6 2013, 19:55:15) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\Animesh>py -2
Python 2.7.4 (default, Apr  6 2013, 19:55:15) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\Animesh>py -3
Fatal Python error: Py_Initialize: unable to load the file system codec
  File "E:\Python27\Lib\encodings\__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax

Current thread 0x00003514 (most recent call first):

I tried a bunch of commands as shown above but python 3.5 does not seem to work. I even tried creating a project on PyCharm using the Python 3.5 interpreter but it gives that same error.

How should I rectify this error?


Solution

  • The official installer for Python will install 3.5 in C:\Program Files\Python 3.5.

    It will automatically add this path to your PATH variable (if you tell it). It seems like you did not enable this option.

    In your PATH (not PYTHONPATH) setting.

    You can get to this by going to the Settings application and searching for environment, then clicking on "Edit environment variables for your account" - this will open a properties window; click Environment Variables on the bottom to load the settings for your account.

    You need to add the path manually to the PATH variable, and remove the E:\Python2.7 path.

    Do this, then restart your command prompt and the default Python version will be 3.5.

    For PyCharm, you can just add a new local interpreter in settings (CTRL+ALT+S) and point it directly to the location where you installed Python 3.5.


    Since both interpreters are named python.exe, the first one found in your PATH will be executed. In practical terms this means, whatever is in your PATH is the default Python for your system. To execute the other version, you need to point to it explicitly.

    If you have Python 2.7 in E:\Python2.7 and Python 3.5 in C:\Program Files\Python 3.5, pick the one you want to be the default and add the path to it in your PATH environment variable - you can always refer to the other installation by typing the full path to the python.exe file.

    C:\>python.exe # this will launch whatever is found first in your `PATH`
    C:\>E:\Python2.7\python.exe  # explicitly launch the 2.7 version.
    

    As far as PyCharm is concerned, it will read PATH and pick up the default interpreter, you can add the other one in your settings by browsing to its location.

    Then, when creating a new project, you can pick which version you want to work with.