Search code examples
pythonpython-module

Transform a python folder into a package: `ModuleNotFoundError`


Suppose that in a folder my_package, i have three files :

  • __init__.py, blanck
  • file1.py, containing a functon function1
  • file2.py, containing the import statement : from file1 import function1

Then, from another directory, when I use import my_package.file2 as file2, I have a ModuleNotFoundError coming from the line of the statement from file1 import function1.

Obviously, i did something wrong somewhere. But where ? The MWE i gave is small, but i append to have a lot of python files in my_package folder, some of them importing each other. I'm trying to change this directory into a package that i can import from elsewhere, that's why i added the __init__.py blanck file, but it does not seem to work that way.


Solution

  • I suppose i found the solution myself : I added a setup.py file with propper content to the folder and then i ran pip -m my_package, allowing me to import it from everywhere..