简体   繁体   中英

import succeeds for one file but not another in the same directory

I have a project folder named proja. It contains several files:

"__init__.py"
classa.py
classb.py
classc.py

I have a subfolder in proja named folderb. It has an "" init .py"", also. This folder contains a py script with the following import statements. It imports classa but fails on classb.

from classa import classa
from classb import classb
from class import classc

It doesn't matter if I run the script from the proja: "python folderb/imptest.py" or of I cd to folderb and run: "python imptest.py".

It always fails with: "ImportError: No module named classb..."

Why will it import from one file but not another in the same location?

Make sure there is a function or method of name classb in the file classb.py

Once run the file with this :

import classa
import classb
import classc

If this commands work then file paths are ok and you should look inside the files.

If you do this and it works check the inside of file classb.py ,
otherwise post the error. It will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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