4

Recently I started working on a new Python project, and I thought I might structure it properly, not throw all the code in one directory like I usually do. Using this tutorial I created a folder structure like this for my project:

myappholder/
myappholder/README.md
myappholder/LICENSE.txt
myappholder/.gitignore
myappholder/myapp/
myappholder/myapp/__init__.py (empty file)
myappholder/myapp/main.py
myappholder/myapp/config.py
myappholder/myapp/subdir
myappholder/myapp/subdir/__init__.py (empty file)
myappholder/myapp/subdir/helper.py

Contents of main.py:

from myapp.subdir.helper import sayHello

Contents of subdir/helper.py:

from myapp.config import SOME_CONST

def sayHello():
    print(SOME_CONST)

Contents of config.py:

SOME_CONST = 'Hello world'

When running python main.py (from myappholder/myapp), I get this error (trace removed for brevity): ModuleNotFoundError: No module named 'myapp. When running python -m main (from myappholder/myapp) I get the same error.

I know it's probably a really simple thing that I'm doing wrong but a few hours of googling did nothing to fix the problem. I'm not sure if the problem is with how I'm setting up the project or how I'm running it but any help will be appreciated.

Details:

  • I'm running Python 3.8 on Windows
  • I'm trying to create a GUI application, not a library
ThatCoolCoder
  • 249
  • 3
  • 16

3 Answers3

4

You should be in your root folder (myappholder) to run everything.

Your directory structure is like this: (when you are inside myappholder)

Write your directory structure in a better fashion, like this:

.
├── myapp
│   ├── config.py
│   ├── __init__.py
│   ├── main.py
│   └── subdir
│       ├── helper.py
│       └── __init__.py
└── README.md

long and full solution

Understand how python imports work. You seem not to know that in detail.

Read this detailed and wonderful article: https://realpython.com/python-import/

concepts that you need to learn:


quick solution for testing you application

Open your terminal and run python (from your project's root directory)

now in this interactive shell, import myapp and call the function you need to run.


exposing a cli

If you need to expose a CLI , you may package your application, and using poetry or setuptools is great.

You could define a command line interface.

For example,if you are using poetry, you can put this in your pyproject.toml

[tool.poetry.scripts]
samplepy = 'samplepy:cli'

now, running, samplepy from the terminal will execute, the cli function of the samplepy package.

aahnik
  • 1,661
  • 1
  • 11
  • 29
  • 1
    I've marked this as accepted because it contains more information than the other two. However, they are both still valid answers. – ThatCoolCoder Apr 14 '21 at 05:30
1

So you're running from myappholder/myapp? Either run from myappfolder or remove the first level of hierarchy (myapp in from myapp.X.X).

am9417
  • 994
  • 8
  • 18
1

in myappholder create a another main_app.py file, in this file call the function/module from myapp/main.py which run the project.

then run the main_app.py script, it will work

sahasrara62
  • 10,069
  • 3
  • 29
  • 44