简体   繁体   English

VS 代码导入错误

[英]Import errors on VS code

I'm new to VS code.我是 VS 代码的新手。 I have downloaded a git project which has demo programs that I can run.我已经下载了一个 git 项目,其中包含可以运行的演示程序。

The directory structure is as follows:目录结构如下:

Projectparser

>demo
>>alg1_demo.py
>>alg2_demo.py
>>alg3_demo.py

>Projectparser

>>alg1
>>>__init__ [ Has just one line : from alg1 import * ]
>>>alg1.py
>>>calg1.c
>>>calg1.h

>>alg2
>>>__init__ [ Has just one line : from alg2 import * ]
>>>alg2.py
>>>calg2.c
>>>calg2.h

>>alg3
>>>__init__ [ Has just one line : from alg3 import * ]
>>>alg3.py
>>>calg3.c
>>>calg3.h

where > indicates sub-directory.其中 > 表示子目录。 Projectparser is the folder from where I open vs code. Projectparser 是我打开 vs 代码的文件夹。 It has a sub-directory by the same name as well which contains all the algorithms I'm interested in.它还有一个同名的子目录,其中包含我感兴趣的所有算法。

When I try running the alg1_demo.py.当我尝试运行 alg1_demo.py 时。 The below line is causing an error.以下行导致错误。

sys.append("../")
from Projectparser import alg1 (line 8) 

I'm getting the following error: ImportError: cannot import name 'alg1' from 'Projectparser' (unknown location)我收到以下错误: ImportError: cannot import name 'alg1' from 'Projectparser' (unknown location)

So I added the line : sys.path.append("../Projectparser")所以我添加了这一行: sys.path.append("../Projectparser")

Then I'm getting the following error :然后我收到以下错误:

File "/home/suneha/Projectparser/demo/alg1_demo.py", line 8, in <module>
    from Projectparser import alg1
  File "../Projectparser/Projectparser/alg1/__init__.py", line 1, in <module>
    from alg1 import *
ModuleNotFoundError: No module named 'alg1'

But the module is present in the subdirectory.但是该模块存在于子目录中。 So I added the line :所以我添加了这一行:

sys.path.append("../Projectparser/Projectparser/alg1")

Then I'm getting this error :然后我收到此错误:

Traceback (most recent call last):
  File "/home/suneha/Projectparser/demo/alg1_demo.py", line 8, in <module>
    from Projectparser import alg1
  File "../Projectparser/Projectparser/alg1/__init__.py", line 1, in <module>
    from alg1 import *
  File "/home//Projectparser/Projectparser/alg1/alg1.py", line 13, in <module>
    from ..logmatch import regexmatch
ImportError: attempted relative import with no known parent package

The same problem persists for all the three algorithms alg1, alg2, alg3.对于所有三种算法 alg1、alg2、alg3,同样的问题仍然存在。 I'm not sure how to fix this and is using sys.append.path() the best way to solve the above mentioned problems.我不确定如何解决这个问题,并且使用 sys.append.path() 是解决上述问题的最佳方法。

Can anyone suggest how to solve the final import error : ImportError: attempted relative import with no known parent package任何人都可以建议如何解决最终的导入错误: ImportError: attempted relative import with no known parent package

and if there is any other compact way of solving the other import errors instead of using sys.path.append().如果有任何其他紧凑的方法来解决其他导入错误而不是使用 sys.path.append()。

Thanks in advance提前致谢

Use the following statement to add the path of the file that needs to be imported to the system path to help VSCode find it:使用如下语句将需要导入的文件的路径添加到系统路径中,以帮助VSCode查找:

 import os,sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

The following is part of the project I created that you provided:以下是您提供的我创建的项目的一部分:

在此处输入图片说明

Result:结果:

在此处输入图片说明

The following points need to be noted:需要注意以下几点:

  1. When importing, the subfolders are connected with .导入时,子文件夹与.

  2. Please avoid using files and folders with the same name to prevent confusion when VSCode finds modules.请避免使用同名的文件和文件夹,以免在 VSCode 查找模块时混淆。

  3. If the result is executable but there is still a wave line, you can add in settings.json : "python.linting.pylintArgs": [ "----extension-pkg-whitelist=1xml" ],如果结果是可执行的但是还是有波浪线,可以在settings.json添加: "python.linting.pylintArgs": [ "----extension-pkg-whitelist=1xml" ],

Update :更新

According to the code of the link you provided, I reproduced the problem you described locally, and the solution is as follows:根据你提供的链接的代码,我在本地复现了你描述的问题,解决方法如下:

  1. Comment out the content " from SLCT import * " of logparser-master\\logparser\\SLCT_ init _.py.注释掉的内容“ from SLCT import * ” LOGPARSER主\\ LOGPARSER \\ SLCT_初始化_.py的。

  2. Add import os,sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) to SLCT_demo.py添加import os,sys sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))SLCT_demo.py

operation result:操作结果:

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM