Python Forum
No module found when I run a main.py
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No module found when I run a main.py
#2
There are different ways to solve this problem. The first thing to understand is that there are two related but distinct hierarchies that must not be mixed up:
  • The file system tree of files and directories.
  • The tree of Python packages and modules.
when you write from A.info import spam it is understood that we are speaking about the tree of Python packages, so A is a Python package and 'info' is a subpackage or a submodule. The import statement does not directly refer to the directories hierarchy.

A very flexible solution to your problem is to build a library containing the modules 'info' and 'country'. Let us call this library 'greatlib'. We are going to package it in a way that allows you to import it from any other program. For this, create a directory hierarchy like this one:
Output:
the_great_lib/ greatlibsrc/ __init__.py country.py info.py setup.py README.md
You can leave the file __init__.py empty but it must exist.

In the file README.md just add the line # Greatlib library for example.

In the file setup.py, create the following content:
from setuptools import setup, find_packages

setup(
    name='greatlib',
    version='0.1.0',
    packages=find_packages(include=['greatlibsrc', 'greatlibsrc.*'])
)
Once you are done, move to the directory the_great_lib in a terminal and type the command (don't forget -e and the dot)
Output:
python -m pip install -e .
That's it, now from any other program, you can use
from greatlib.info import Person
Reply


Messages In This Thread
No module found when I run a main.py - by tomtom - Jul-20-2022, 07:32 AM
RE: No module found when I run a main.py - by Gribouillis - Jul-20-2022, 09:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Upgraded Python: Module no longer found - for Linux Curbie 8 1,506 Mar-05-2025, 06:01 PM
Last Post: Curbie
Question [SOLVED] Upgraded Python: Module no longer found Winfried 1 1,135 Jan-01-2025, 02:43 PM
Last Post: Larz60+
  Module not found error even though installed NZGeorge 1 4,842 Jul-10-2024, 09:08 AM
Last Post: Larz60+
  pyside6 module not found ForeverNoob 4 6,109 Aug-18-2023, 04:36 PM
Last Post: snippsat
  Module Not Found Error bitoded 4 2,625 Jan-01-2023, 09:08 AM
Last Post: bitoded
  pdfminer package: module isn't found Pavel_47 25 17,221 Sep-18-2022, 08:40 PM
Last Post: Larz60+
  Module not found question sighhhh12 0 2,179 Sep-09-2022, 05:43 AM
Last Post: sighhhh12
  [SOLVED] Tkinter module not found Milan 7 50,539 Aug-05-2022, 09:45 PM
Last Post: woooee
  No Module found in other directory than source code [SOLVED] AlphaInc 1 3,258 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  Error when refering to class defined in 'main' in an imported module HeRo 2 3,255 Apr-13-2021, 07:22 PM
Last Post: HeRo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020