import packages not work python 3 -
i create packages in python 3 , went try import in main folder not work
my architecture
--mainfolder --__init__.py --start.py --packages --__init__.py --file1.py --file2.py when start programme console this
python3 start.py i have error
traceback (most recent call last): file "start.py", line 8, in <module> packages import class1 importerror: cannot import name 'class1' my fist init file in mainfolder empty
in init in packages try
1 from .file1 import class1
2 from . import class1
3 from . import file1
in start.py try call module in many ways
1 from packages import class1
2from .packages import class1
3from . import class1
edit
in packages/file1 code this
import time import sys class class1(objet): def run(self): print('test') in call file in start.py this
class1().run() i try how import module sub directory
this cannot import modules in python3 sub directory
and importing module sub-directory works in python 2, not python 3
i think file init not load because went add
sys.path.append('path/to/my/package') in import packages work pycharm give me , error each time , not compile script
as @john gordon said:
if class1 in file1.py can import with
from packages.file1 import class1 you can import class1 packages/init.py with
from file1 import class1 then import straight packages
from packages import class1
Comments
Post a Comment