[Python] How to package a module in Python 3?

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am struggling to find a way to package a module in Python 3. My package is not being recognised as such, meaning the module cannot be imported.

    As an example, I do the following in the same order:


    1. I created a package folder.


    2. I created the module.py scripts and the __init__.py script inside the package folder.


    3. I wrote the given code in the module.py file.

      # Module content
      def sample_method():
      return "message"

    4. I read the __init__.py file could be empty at first.

      # empty file

    5. I called the module from the python3 interpreter.

      python3
      import module as m
      print(m.method())

    6. It works well when I execute it from the Python interpreter at the TTY terminal. However, when I try to call the method from a python_script.py file located outside the package, no error is thrown; instead, any kind of error, instead the import statement is marked as not finding the given module.

    Import "custom_modules" could not be resolved

    How to solve this problem?

    Continue reading...

Compartilhe esta Página