I create a packages in python 3 and went a try to import this in my main folder this is not work
My architecture like this
--mainFolder
--__init__.py
--start.py
--packages
--__init__.py
--file1.py
--file2.py
when i start my programme with the console like this
python3 start.py
i have this error
Traceback (most recent call last):
File "start.py", line 8, in <module>
from packages import Class1
ImportError: cannot import name 'Class1'
my fist init file in my mainFolder is empty
in my init in my packages i try
1 from .file1 import Class1
2 from . import Class1
from . import Class1
3 from . import file1
from . import file1
in my start.py i try to call my module in many ways
1 from packages import Class1
2 from .packages import Class1
3 from . import Class1
from . import Class1
EDIT
in my packages/file1
my code like this
import time
import sys
Class Class1(objet):
def run(self):
print('test')
in a call this file in my start.py like this
Class1().run()
i try this How to import a module from sub directory
this Cannot import modules in Python3 from sub directory
and this Importing module from sub-directory works in Python 2, but not Python 3
I think my file init not load because went i add this
sys.path.append('path/to/my/package')
in import my packages its work but pycharm give me and error each time and not compile the script
As @John Gordon said:
If Class1 is in file1.py then you can import it with
from packages.file1 import Class1
You can also import Class1 into packages/ init .py with
from file1 import Class1
Then import straight from packages
from packages import Class1
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.