Vladimir Shebuniayeu
Vladimir Shebuniayeu

Reputation: 344

Python import from folder

I have such structure of application

mainApp.py
   |_Folder1
       |_fileFromFolder1.py
   |_Folder2
       |_fileFromFolder2.py

I want in fileFromFolder1.py import object from fileFromFolder2.py

I know that one of solution is to sys.path.insert(0, '/path/to/application/app/folder') but is it possible to make without absolute path

Upvotes: 0

Views: 144

Answers (1)

Sebastian Wozny
Sebastian Wozny

Reputation: 17516

You need(not strictly need in Python3, but still really really should have) __init__.py files in directories you would like to import code from. An __init__.py file makes the difference between a plain directory and a Python package

mainApp.py
   |_Folder1
       |__init__.py
       |_fileFromFolder1.py
   |_Folder2
       |__init__.py
       |_fileFromFolder2.py

Upvotes: 1

Related Questions