问题描述
我有一个 PyCharms python 项目配置为在虚拟环境(Python 2.7.9)上运行.关闭晚了,尝试在 PyCharms 上调试项目,该项目用于引发许多模块的导入错误.当从运行相同虚拟环境的命令提示符调用或从 PyCharms运行"项目时,代码工作得很好.经过检查,我发现由于某种原因 PyCharms 正在调用 Python 2.7.10,并且可能没有安装第三方依赖项!
I have a PyCharms python project configured to run on a virtual env (Python 2.7.9).Off late , trying to debug the project on PyCharms used to throw import errors for many modules. The code works just fine when invoked from a command prompt running same virtual env or while "Run"ing the project from PyCharms. Upon inspection I found that for some reason PyCharms is invoking Python 2.7.10 and probably the third party dependencies are not installed in there !.
我的问题是,在调用子进程时 PyCharms 是否有理由远离虚拟环境,以及克服这个问题的最佳方法是什么.
My Question is, Is there a reason why PyCharms is moving away from the virtual env when invoking subprocess and what would be the best way to overcome this problem.
这是从控制台/pycharms运行命令时主进程和子进程的输出
Here is the output of main process and subprocess when running from console/pycharms for the command
print __import__("sys").version
Sys version from Main Process!
2.7.9 (default, Feb 10 2015, 03:28:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]
Sys version from Sub Process!
2.7.9 (default, Feb 10 2015, 03:28:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]
这是从 PyCharms 调试时的输出
And this is the output while debugging from the PyCHarms
print __import__("sys").version
Sys version from Main Process!
2.7.9 (default, Feb 10 2015, 03:28:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]
Sys version from Sub Process!
2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)]
PyCharms 版本:PyCharms 社区版 4.5.3
PyCharms version:PyCharms Community edition 4.5.3
推荐答案
Update with MacOSX ElCaptian update
随着 OSX ElCaptian 引入新的无根功能(
Update with MacOSX ElCaptian update
With the introduction of new rootless feature with OSX ElCaptian (https://apple.stackexchange.com/questions/193368/what-is-the-rootless-feature-in-el-capitan-really) , it is no longer possible to alter python at /usr/bin , instead the better approach would be to just update PATH env variable from run configuration (Run->Edit Configuration->Environment->EnvironmentVariable and add/update PATH variable to /usr/local/bin:$PATH)
稍加修改,我就解决了这个问题,可能这不是正确的方法,但它确实为我解决了问题.
With little tinkering I was able to resolve the issue , may be it's not the correct way but it did solve the issue for me.
PyCharms 在调试时以某种方式使用了从/usr/bin/调用 python 的 python 2.7.10 而不是来自/usr/local/bin 的 python 2.7.9 .由于我所有的路径设置都是正确的并且我不依赖 2.7.10 ,我将/usr/bin/"中的 python 重命名为 python2.7.10 并在/usr/local/bin"中创建了一个到 python 的符号链接/bin"文件夹.
PyCharms while debugging somehow used invoke python from /usr/bin/ having python 2.7.10 instead of python 2.7.9 from /usr/local/bin . Since All my path settings are correct and I do not rely upon 2.7.10 ,I rename python in "/usr/bin/" to python2.7.10 and created a symbolic link to python from /usr/local/bin in "/usr/bin" folder.
PyCharms 现在像 char 一样工作,但我仍然不明白为什么它过去常常引用绕过 PATH 设置的外部 python,以及为什么它在运行子进程时必须在 virtualenv 之外调用 python 解释器.对这些问题的任何想法或提示表示高度赞赏.
PyCharms now works like charm , but I still do not understand why it used to refer an external python bypassing the PATH settings and more-over why it has to invoke a python interpreter outside virtualenv when running a subprocess. Any idea's or hints on these questions are highly appreciated.
这篇关于Pycharms 调试器在子进程中的不同 Python 解释器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!