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
#1
I have info.py file in directory A

info.py has this code:
import country # country is imported from the same directory A

class Person:
    def get_city(self):
        return country.state()
Then i have a main.py that is not in directory A
main.py has this code:
from A.info import Person

person_instance = Person()
print(person_instance.get_city())
Then I will get module country not found error,
but if I instanciate the Person class in the same directory A or copy the country.py to the main.py directory it will work fine

How can I make the import of country work as I'm calling Person class in main.py that is in another directory?
the code here is just for explanation purpose only.

Your answers is highly appreciated in advance!
Reply
#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
#3
(Jul-20-2022, 09:13 AM)Gribouillis Wrote: 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

Thank you so much, I'm going to impliment it right now. More power to you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyside6 module not found ForeverNoob 4 1,273 Aug-18-2023, 04:36 PM
Last Post: snippsat
  Module Not Found Error bitoded 4 1,344 Jan-01-2023, 09:08 AM
Last Post: bitoded
  pdfminer package: module isn't found Pavel_47 25 8,413 Sep-18-2022, 08:40 PM
Last Post: Larz60+
  Module not found question sighhhh12 0 1,447 Sep-09-2022, 05:43 AM
Last Post: sighhhh12
  [SOLVED] Tkinter module not found Milan 7 21,480 Aug-05-2022, 09:45 PM
Last Post: woooee
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,004 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,334 Apr-13-2021, 07:22 PM
Last Post: HeRo
  KafkaUtils module not found on spark 3 pyspark aupres 2 7,258 Feb-17-2021, 09:40 AM
Last Post: Larz60+
  'urllib3' Module not found when import 'requests' spanz 5 9,961 Jan-06-2021, 05:57 PM
Last Post: snippsat
  no module named finbert found ErnestTBass 4 4,488 Dec-05-2020, 06:09 PM
Last Post: andrianas

Forum Jump:

User Panel Messages

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