views:

75

answers:

3

The story began with a very strange error while I was running my script from PyDev. Running the same script from outside will not encounter the same problem.

Fatal Python error: Py_Initialize: can't initialize sys standard streams
  File "C:\Python26\lib\encodings\__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

I was able to find why this is happening: In PyDev I use two different Python versions: 3.1 that is the default installation and 2.6 as the alternative one.

My Windows Environment does not contains PYTHONHOME, CLASSPATH, PYTHONPATH but PyDev does add them.

Now the problem is at one stage my python script does execute another python script using os.system(python second.py) and the second script will fail with the above error.

Now I'm looking to find a way to prevent this issue, issue that is happening because it will run the execute the default python using the settings for the non-default one (added by PyDev).

I do not want to change the standard call (python file.py) but I want to be able to run my script from pydev without problem and being able to use default or alternative python environment.

Any ideas?

A: 

I may not be understanding this quite right, but I think you're invoking a script from pydev that works okay, but this script executes another script which requires a different version.

While this would unfortunately be installation-specific, you could use os.system("c:\absolute\path\to\proper\version\of\python.exe second.py").

If PyDev is setting up conflicting environmental variables, you may want to look into subprocess over os.system.

http://docs.python.org/library/subprocess.html#using-the-subprocess-module

This will allow you to invoke a process with a handle, so you can optionally wait for it to terminate. It will also allow you to pass environment variables upon execution.

andyortlieb
First, calling python with full path is unacceptable. Second, the second script does not require a specific python version but because it does run the default pyhton interpretor with another python ENV settings it will just fail (python will fail). I already found a solution, to allways call this python script with -E, meaning it will ignore PYTHONPATH.
Sorin Sbarnea
A: 

I found a solution that seams acceptable specially because it will not interfere with running the scripts on other systems, just to run python -E second.py - this will force Python to ignore PYTHON* environment variables.

Sorin Sbarnea
A: 

I believe your call should be:

import sys

os.system(sys.executable+ ' second.py')

So that you guarantee you're using the same interpreter you're currently running and not launching the other one (or did you really mean to use the other interpreter?)

Fabio Zadrozny