0

Yes I read the answer to this Relative imports for the billionth time. No I did not find it really useful as I tried both solutions and it did not work for me. See below.

I have the below folder structure.

(I do not have any __init__.py, as I understood they're not required anymore. I have Python 3.7)

project
├── subpackage1
└── subpackage2
    ├── launch
    |   └── launchFunc.py
    |   └── launchFunc.sh
    └── src
        └── somemodule.py

launchFunc.py:

from somemodule import func

func()

launchFunc.sh:

#!/bin/bash
python -m launch.launchFunc

Then I open command line and I cd into project/subpackage2/ and I run the below command:

./launch/launchFunc.sh

and I get the below error:

from somemodule import func
ModuleNotFoundError: No module named 'somemodule'

If I replace from somemodule import func with from ..src.somemodule import func, I get:

ValueError: attempted relative import beyond top-level package

What is the correct way to import and/or run scripts in python ? Isn't there a way to have all functions automatically imported to all required modules ?

1 Answers1

0

Ok I have found out that by:

  • always using the full path to import: import func from subpackage2.somemodule
  • always running things from the project folder it will work fine.

I think this is a rule I should adopt for all my projects.