[Python] How to run a Python code inside a custom package?

Discussão em 'Python' iniciado por Stack, Fevereiro 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using Visual Studio Code.

    Suppose my folder looks like:

    ├── main.py
    └── package
    ├──__init__.py
    ├──utils.py
    └──method.py


    In my method.py, I import the utils.py, which is in the same directory, so I put the dot before the name:

    from .utils import *


    then I can run the script in main.py like:

    from package import method


    This will work. But the question is, how I can run the script in method.py at its directory instead of importing it in main.py? If I run the script method.py directly, an error will occur:

    ModuleNotFoundError: No module named '__main__.modules'; '__main__' is not a package


    What can I do to run the script in method.py without removing the dot as from utils import *?

    Continue reading...

Compartilhe esta Página