1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How do I import package when running

Discussão em 'Python' iniciado por Stack, Setembro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have a directory structure that can be simplified like this:

    test/
    scripts/
    a.py
    src/
    b.py


    In a.py, I have the line:

    import src.b

    If I'm in the test directory, I want this to work:

    python3 scripts/a.py

    If I'm in the scripts directory, I want this to work:

    python3 a.py

    How can I accomplish this?

    If I move a.py to the top level, it works. I don't want to call the script using python -m scripts.a.

    Do I need to import sys and add '..'? Do I need an init.py in the src directory? Do I need relative imports using import ..src? Does the current directory vs the script directory matter?

    Continue reading...

Compartilhe esta Página