0

I tried different methods shown on multiple sites but couldnt figure out the problem. I need to import "demo1.py" in "demo2.py". Here is my folder hierarchy:

Project:___ __init__.py
 |_ main.py
 |_ Package1:___ __init__.py
 | |_ demo1.py
 |_ Package2:___ __init__.py
 |_ demo2.py

When I have this in main.py, it works.

import Package1.demo1
import Package2.demo2

But this doesnt work in demo2.py

import Package1.demo1

Neither does this:

import Project.Package1.demo1

I tried following this tutorial https://www.programiz.com/python-programming/package but Im doing something wrong. Tried renaming "main.py" to something else in case it is causing problems but that doesnt helped either.

Edit: Solution found

from sys import path 
import os
par_dir = os.path.abspath(os.path.join(os.path.dirname( __file__ ), os.pardir))
path.append(par_dir)
import Package1.demo
asked Apr 29, 2019 at 7:29
2

1 Answer 1

1
from sys import path
path.append("project-path")

Define it inside of demo1 or demo2, it will work. You may check this question.

answered Apr 29, 2019 at 7:35
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.