Suneha K S
Suneha K S

Reputation: 362

Import errors on VS code

I'm new to VS code. I have downloaded a git project which has demo programs that I can run.

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. 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. 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)

So I added the line : 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. I'm not sure how to fix this and is using sys.append.path() the best way to solve the above mentioned problems.

Can anyone suggest how to solve the final import error : 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().

Thanks in advance

Upvotes: 1

Views: 2086

Answers (1)

Jill Cheng
Jill Cheng

Reputation: 10354

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:

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:

enter image description here

Result:

enter image description here

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.

  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" ],

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.

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

operation result:

enter image description here

Upvotes: 1

Related Questions