ImportError Using Modules in Parent Directory

Asked 2 years ago, Updated 2 years ago, 44 views

Environment Windows 10
Language python 3.5

From a command prompt
a program written in python Even if I try, I get an error

 C:\Users\user\gv\ana>python AAA.py
Traceback (most recent call last):
  File "AAA.py", line 1, in <module>
    from lib import BBB
ImportError: cannot import name 'BBB'

The error appears.
(My current directory is ana, and there is also a file called AAA.py)
I changed the environment variable, but it didn't work.
I can't think of the next move at the moment.
By the way, it works on other computers.
Therefore, it does not seem to be a source code issue.
Please let me know.

C:.
--ana
│  -- AAA.py
|  --_init__.py 
|
--lib
  └- └─ BBB.py

What's Inside AAA.py?

from lib import BBB

That's all.Thank you for your cooperation.

python

2022-09-30 21:21

1 Answers

The path to the parent directory is not visible from AAA.py, so how about adding it to sys.path? 

import sys

sys.path.append('../')
from lib import BBB


2022-09-30 21:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.