1

I'm looking to create a python module with the following imports. However, I'm not sure how to go about structuring the files / directories to get the desired effect?

import one
from one import two
from one.two import three
from one.two.three import four

Obviously these imports wont exist in the same file like they are shown above, but I'm looking to have the flexibility to import in this way.

asked Jul 2, 2014 at 23:50

3 Answers 3

1
main/
 one/
 __init__.py
 two/
 __init__.py
 three.py

inside three.py create a function four, something like:

def four():
 print ('this is four')

Now add path of main directory to the PYTHON_PATH

Note: you need empty __init__.py files in every folder, so that python track those folders.

answered Jul 2, 2014 at 23:57
Sign up to request clarification or add additional context in comments.

Comments

0

What you have to do is set up your directory tree with regular folders first. So you would have folder three in folder two in folder one. Then in each folder place an empty python file called

__init__.py 

which will tell python to treat this as a package folder. It should then work the way you want it to.

Read more here: https://docs.python.org/3/tutorial/modules.html#packages

answered Jul 2, 2014 at 23:57

Comments

0

i hope this would help, for any module directory

__init__.py

file is needed, you may leave it empty but it is needed. for any other file create .py version of it, for example: import one.example would need an example.py file inside "one" directory. hope this is what you were looking for:

|__ one
 |
 |__ __init__.py
 |
 |__two
 |
 |__ __init__.py
 |
 |__three
 |
 |__ __init__.py
 |
 |__four
 |
 |__ __init__.py
answered Jul 3, 2014 at 0:00

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.