开发者

How to properly use PyDev with two different Python versions with scripts that are recalling other python scripts?

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: inva开发者_开发百科lid 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?


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.


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.


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?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜