简体   繁体   English

仅在PyCharm中尝试了相对顶级包消息以外的相对导入

[英]attempted relative import beyond top-level package message in PyCharm only

UPDATE: if I change the name of the select module (in which the import error occurs) to select1 , the problem goes away! 更新:如果我将select模块的名称(发生导入错误)更改为select1 ,问题就解决了! WHAT the? 什么?

First off: everything works fine when I try to import my package from the command line, or run pytest in the project directory. 首先,当我尝试从命令行导入软件包或在项目目录中运行pytest时,一切正常。

But in PyCharm I'm getting the "attempted relative import beyond top-level package" problem when I try to run my tests, and ONLY while in PyCharm. 但是在PyCharm中,尝试运行测试时会遇到“尝试超出顶级程序包的相对导入”的问题,并且仅在PyCharm中出现。 I can't figure out why. 我不知道为什么。

I have even deleted and re-cloned the repository and the problem still occurs. 我什至删除并重新克隆了存储库,问题仍然存在。

Here is the link to the problem branch. 这是问题分支的链接

And here is the tb: 这是TB:

Testing started at 4:02 PM ...
C:\Users\ricky\AppData\Local\Programs\Python\Python37\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.2\helpers\pycharm\_jb_pytest_runner.py" --path C:/Users/ricky/PycharmProjects/candejar/tests
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.2\helpers\pycharm\_jb_pytest_runner.py", line 10, in <module>
    from pkg_resources import iter_entry_points
  File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\site-packages\pkg_resources\__init__.py", line 36, in <module>
    import email.parser
  File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\email\parser.py", line 12, in <module>
    from email.feedparser import FeedParser, BytesFeedParser
  File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\email\feedparser.py", line 27, in <module>
    from email._policybase import compat32
  File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\email\_policybase.py", line 9, in <module>
    from email.utils import _has_surrogates
  File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\email\utils.py", line 29, in <module>
    import socket
  File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\socket.py", line 52, in <module>
    import os, sys, io, selectors
  File "C:\Users\ricky\AppData\Local\Programs\Python\Python37\lib\selectors.py", line 12, in <module>
    import select
  File "C:\Users\ricky\PycharmProjects\candejar\candejar\select.py", line 24, in <module>
    from . import exc
ImportError: attempted relative import with no known parent package

Process finished with exit code 1

I'm sure there is something simple going on here but I just can't figure it out. 我敢肯定,这里发生了一些简单的事情,但我只是想不通。 NOTE: I'm using Python 3.7 RC 1. 注意:我正在使用Python 3.7 RC 1。

The sys.path in pycharm and the command line come out to be the same, except pycharm also includes the pycharm project main package directory (which for some reason appears twice ). pycharm和命令行中的sys.path相同,只是pycharm还包括pycharm项目主软件包目录(由于某种原因,它出现了两次 )。

I was able to solve this by deleting the .idea directory from the cloned repository and re-opening PyCharm. 我能够通过从克隆的存储库中删除.idea目录并重新打开PyCharm来解决此问题。 I still don't know what was wrong, but it is fixed now. 我仍然不知道出什么问题了,但现在已解决。 Very weird. 很奇怪。

UPDATE: it has something to do with the following line in the project .iml file in the .idea folder: 更新:它与.idea文件夹中的项目.iml文件中的以下行有关:

<sourceFolder url="file://$MODULE_DIR$/candejar" isTestSource="false" />

Removing that xml code solves the issue. 删除该xml代码即可解决此问题。 But I do not know why, and I don't know how it got there in the first place (I do not recall messing around with any of my settings prior to this error). 但是我不知道为什么,而且我也不知道它是如何到达那里的(我不记得在发生此错误之前弄乱了我的任何设置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尽管PyCharm具有自动填充功能,但ValueError尝试了相对导入而不是顶级软件包 - ValueError attempted relative import beyond top-level package, despite PyCharm autofilling suggestions ValueError:尝试在顶级包python之外进行相对导入 - ValueError: attempted relative import beyond top-level package python ImportEror:尝试相对导入超出顶级包 - ImportEror: attempted relative import beyond top-level package 值错误:尝试相对导入超出顶级 package - Value Error: Attempted relative import beyond top-level package 无法修复:尝试相对导入超出顶级 package - cannot fix: attempted relative import beyond top-level package Python 错误:尝试相对导入超出顶级 package - Python error: attempted relative import beyond top-level package “尝试相对导入超出顶级包” - “Attempted relative import beyond top-level package” 尝试在顶级包 python 之外进行相对导入 - attempted relative import beyond top-level package python ValueError:尝试相对导入超出顶级包(Scrapy) - ValueError: attempted relative import beyond top-level package (Scrapy) 导入错误:尝试在 Django 中的顶级包之外进行相对导入 - ImportError: attempted relative import beyond top-level package in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM