PyCharm如何以及为何改变Python的导入逻辑? [英] How and why does PyCharm alter Python's import logic?

查看:196
本文介绍了PyCharm如何以及为何改变Python的导入逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上有一个PyCharm 1.2.1。我必须编辑同一个项目的不同分支 - 有点重的分支,因为它们位于磁盘上的不同文件夹中 - 比如它是 c:\ apps \alpha C:\apps\beta 。对于每个这样的文件夹,应调整PYTHONPATH变量以指向位于项目根文件夹内的适当供应商库( c:\alpha\vendor ,...)。我设法通过从cmd批处理文件启动PyCharm解决了这个问题,该文件在启动PyCharm之前设置了必要的PYTHONPATH变量值(所以我在里面有 PyCharm.cmd alpha PyCharm.cmd beta 目录内。

I have a PyCharm 1.2.1 on Windows. I have to edit different branches of the same project - somewhat heavy branches, because they live in different folders on disk - say it to be c:\apps\alpha and c:\apps\beta. For each such folder PYTHONPATH variable should be adjusted to point to appropriate vendor libraries which live somewhere inside project root folder (c:\alpha\vendor,...). I managed to work around this issue by starting PyCharm from cmd batch file which sets necessary PYTHONPATH variable value before starting PyCharm (so I have PyCharm.cmd inside alpha and PyCharm.cmd inside beta directories).

问题:当我打开PyCharm编辑 beta 文件夹时, import 语句 alpha 文件夹导入模块!当我从命令行启动 python.exe 解释器时,一切都按预期工作 - 问题似乎只有只有PyCharm

Problem: when I open PyCharm for editing beta folder, import statement imports modules from alpha folder! When I start python.exe interpreter from command line everything works as expected - the problem seems to be only with PyCharm.

第二个奇怪的调查:从PyCharm执行 print sys.path 代码并从外部python.exe解释器给出不同的结果 - 来自PyCharm它没有展示我的PYTHONPATH模块。我是Python和PyCharm的新手,但这种行为看起来非常奇怪且不一致。

Second strange investigation: executing print sys.path code from PyCharm and from external python.exe interpreter give different results - from PyCharm it do not show my PYTHONPATH modules. I'm a newcomer in Python and PyCharm, but this behavior looks very strange and inconsistent.

最后我在PyCharm IDE中发现了一些看起来应该允许我的设置解决此问题 - 文件 - >设置 - > Python解释器。在那里,我找到了 alpha beta 文件夹的所有路径。无法清除此路径列表,只能禁用某些元素。但即使是禁用也有点愚蠢 - 我应该每次编辑 alpha beta 时单独禁用/重新启用它们解决方案文件夹。

Finally I've found some settings in PyCharm IDE that looks like should allow me to resolve this issue - File -> Settings -> Python Interpreter. There I've found all paths for both alpha and beta folders. There is no way to clear this paths list, only to disable some elements. But even disabling works somewhat stupidly - I should disable/re-enable them separately for each time I edit alpha or beta "solution folders".

所以最后一个问题:如何使用PyCharm编辑单独的项目(文件夹)并强制PyCharm不从错误的位置导入模块?

So final question: how one can with PyCharm edit separate projects (folders) and force PyCharm not to import modules from wrong locations?

推荐答案

正确的解决方案不是试图解决PyCharm的逻辑,而是在设计时使用IDE功能。这意味着:

The correct solution is not to try to work around PyCharm's logic, but rather to use the IDE features as they're designed. That means:


  • 不使用任何.cmd脚本启动PyCharm

  • 删除所有路径特定于设置|中路径列表中的alpha和beta Python解释器(如果有的话)列在那里

  • 将alpha / vendor和beta / vendor目录分别标记为alpha和beta项目的源根。

然后PyCharm将正确解析导入并在从中运行应用程序时自动生成正确的PYTHONPATH。

Then PyCharm will resolve imports correctly and generate the correct PYTHONPATH automatically when running the application from it.

这篇关于PyCharm如何以及为何改变Python的导入逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆