0

My project structure is the following. Inside api.py i need some functions written in the upper level.

Project1
├── model.py 
├── audio_utils.py
├── audio.py 
└── backend
 ├── static
 │ ├──js
 │ ├──img
 └── api.py

Why am I unable to import inside api.py the functions in the upper level? When i try to do:

from audio_utils import *

I got the following:

 No module named 'audio_utils'
asked Sep 21, 2020 at 12:00
2
  • Have you tried from Project1.audio_utils import *? Commented Sep 21, 2020 at 12:02
  • Can you check if the path project is in PYTHONPATH python -c "import sys;print(sys.path);" Commented Sep 21, 2020 at 12:16

2 Answers 2

1

Modules are imported from paths prefixes specified in sys.path. It usually contains '' that means that modules from current working directory are gonna be loaded. (https://docs.python.org/3/tutorial/modules.html#packages)

I think you are starting your Python interpret while being in the backend directory. Then I think there is no way to access the modules in the upper directory -- not even with the .. (https://realpython.com/absolute-vs-relative-python-imports/#syntax-and-practical-examples_1) unless you change the sys.path which would be a really messy solution.

I suggest you create __init__.py files to indicate that the directories containing them are Python packages:

Project1
├── model.py 
├── audio_utils.py
├── audio.py 
└── backend
 |-- __init__.py
 ├── static
 │ ├──js
 │ ├──img
 └── api.py

And always start the interpret from the Project1 dir. Doing so, you should be able to import any module like this:

import model
from backed import api
import audio_utils

no matter in which module in the Project1 you are writing this in. The current directory of the interpret will be tried.

Note there is also the PYTHONPATH env variable and that you can use to your advantage.

Note that for publishing your project it is encouraged to put all the modules in a package (in other words: don't put the modues to the top level). This is to help prevent name collisions. I think this may help you to understand: https://realpython.com/pypi-publish-python-package/

answered Sep 21, 2020 at 12:34
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I created a new file in the top folder than imported the api form backend and launched the server as usual. It was not working before because my 'main' was inside the api.py
0

If you create the dir structure this way:

$ tree
.
├── bar
│  ├── den.py
│  └── __init__.py # This indicates the bar is python package.
└── baz.py
1 directory, 3 files
$ cat bar/den.py 
import baz

Then in the dir containing the bar/ and baz.py (the top level) you can start the Python interpret and use the absolute imports:

In [1]: import bar.den 
In [2]: import baz 
In [3]: bar.den.baz 
Out[3]: <module 'baz' from '/tmp/Project1/baz.py'>

As you can see, we were able to import bar.den which also could import the baz from the top-level.


 
answered Sep 21, 2020 at 13:33

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.