19

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 3.3.

For Example: if I type F:\Python33\python33.exe test1.py, it will run with 3.3 and work fine, but if I type F:\Python27\python27.exe test1.py it gives this error:

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

Note: I have renamed the Python 2.7 and 3.3 .exe's to python27.exe and python33.exe respectively.

Any help would be appreciated, Thank you.

3
  • 1
    Have you set PYTHONPATH as an environment variable? Commented Dec 12, 2013 at 22:24
  • My PYTHONPATH is F:\PYTHON27;F:\PYTHON27\DLLs;F:\PYTHON27\LIB;F:\PYTH Commented Dec 12, 2013 at 22:28
  • Just one offtopic sugestion. Use linux preferably for python development, much easier and better. Easier to have multiple python versions and environments, easier and better terminal, easier to compiler c extensions, open source and free... Commented Dec 12, 2013 at 22:29

7 Answers 7

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Note: Hashbang (#!) is ignored on Windows. You can't use it switch Python versions (but it's still a good idea to show which version you expect).
@AaronDigulla Is there any equivalent functionality on Windows?
Not that I'm aware of. You can install MinWG to get a unix-like environment but it's just an emulation and you'll notice a few oddities. Maybe ask a new question?
The Shebang (#!) works if you use the py launcher.
3

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 enter image description here

1 Comment

i got this error when it wrong call another site.py and then i resolve this error by move python to the top of path.
2

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

Comments

0

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

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

Comments

0

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.

Comments

0

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

Comments

-2

I got this fixed by removing the = sign and simply keeping as sys.stderr

1 Comment

Do you mean you modified F:\PYTHON33\LIB\site.py? I wouldn't recommend changing code inside the Python installation...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.