pycharm 2018 中的相对导入不像 python 3.6 中的相对导入那样工作 [英] relative import in pycharm 2018 does not work like relative import in python 3.6

查看:60
本文介绍了pycharm 2018 中的相对导入不像 python 3.6 中的相对导入那样工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于 python 中相对导入的无休止的讨论,我认为它如此令人困惑的原因之一是它从一个 Python 版本更改为另一个(我的版本是 3.6).但这里的罪魁祸首似乎是 PyCharm(除非我弄错了..),我想知道是否有人遇到过这个问题的解决方案.对于具有此布局的项目:

I have read endless discussions on relative import in python, I think that one of the reasons it is so confusing is that it changes fro one Python version to another (my version is 3.6). But here the culprit seems to be PyCharm (unless i'm mistaken..) and i wonder if anyone came across a solution to this issue. for a project with this layout:

/project
    |-- __init__.py
    |---subfolder
            |-- __init__.py
            |-- AA.py
            |-- BB.py

假设 AA.py 包含一些函数 myfunc在文件 BB.py 内,如果我写这个导入:

Let's imagine that AA.py contains some fuction myfunc inside the file BB.py if i write this import:

from AA import myfunc

然后 python 工作完美,但 PyCharm 将其视为错误:

Then python works perfectly, but PyCharm sees it as an error:

为了让 PyC​​harm 开心,我可以将 . 添加到导入中,然后错误似乎已解决:

So to make PyCharm happy i can add the . to the import and then the error is seemingly resolved:

from .AA import myfunc

但是python不高兴,给了我错误:ModuleNotFoundError: No module named '__main__.AA';'__main__' 不是包

But then python is not happy, giving me the error: ModuleNotFoundError: No module named '__main__.AA'; '__main__' is not a package

总而言之,我使用了实际有效的导入(即 from AA import myfunc),但如果我能让 PyC​​harm 以某种方式同意它,那就太好了,因为它提供了诸如 auto 之类的功能完成,转到定义等.

So to conclude, I use the import that actually works (i.e. from AA import myfunc) but it would be great if i could make PyCharm agree to it somehow because then it offers features such as auto complete, go to definition and so on.

不重复:我知道这个话题似乎被反复讨论过,但它也有很多方面.我在这里谈论的是 pycharm 方面,因此据我所知,这个话题是新的.

not duplicates: I know it seems like this subject was discussed over and over but it also has many aspects. Here i'm talking about the pycharm aspect and therefore this topic is new as far as i know.

  1. PyCharm 如何处理模块的相对导入?- 是没有将根项目目录添加到 PYTHONPATH 的用户
  2. Pycharm 自动相对导入 - 谈论的不是这种情况的自动导入功能这里
  3. PyCharm 中的子包和相关导入 - 正在讨论中的导入问题python 2.7,但在这里我没有任何导入问题
  4. 第十亿次的相对进口 - 提供了对进口的精彩评论问题并且还提供了非常详细的答案 - 对我来说没有任何帮助,因为我没有任何导入问题.更不用说它是python 2.7主题而不是3.x
  1. How does PyCharm handle relative imports of modules? - is a user who didn't add the root project dir to PYTHONPATH
  2. Pycharm auto relative imports - is talking about auto-import feature that is not the case here
  3. Subpackages and relative imports in PyCharm - is talking about import issue in python 2.7 but in here i'm not having any issue to import
  4. Relative imports for the billionth time - offeres a great review of importing issues and also features a very detailed answer - none of it helps in my case because i don't have any import issue. not to mention that it is python 2.7 topic and not 3.x

推荐答案

在项目树中右键单击将 subfolder 标记为 Source Root -> Mark directory as ... -> Sources Root.默认情况下,PyCharm 将所有 Source Roots 添加到 PYTHONPATH 中,因此问题应该得到解决

Mark subfolder as Source Root with right-click in the project tree -> Mark directory as ... -> Sources Root. PyCharm adds all Source Roots to PYTHONPATH by default so the issue should be resolved

问题是 PyCharm 不知道您将直接执行 BB.py,例如假设您在根目录中有 main.py,其中 from subfolder import BB.调用 python main.py 将引发 ModuleNotFoundError: No module named 'AA'(确保使用 Python 3 以避免从 Python 2 隐式相对导入).

The problem is PyCharm doesn't know you are going to execute BB.py directly, e.g. let's say you have main.py in the root with from subfolder import BB. Calling python main.py will raise ModuleNotFoundError: No module named 'AA' (make sure to use Python 3 to avoid implicit relative imports from Python 2).

希望它是有道理的,我没有错过任何东西.

Hope it makes sense and I didn't miss anything.

这篇关于pycharm 2018 中的相对导入不像 python 3.6 中的相对导入那样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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