I have the following directory structure
parent_directory/
+-- utils.py
+-- child_directory/
| +-- script.py
+-- results_directory/
| +-- results1
| +-- results2
There are several scripts in child_directory/
that do not import anything from each other. However all of them import stuff from utils.py
in the parent_directory/
and use data from results_directory
. Instead of copying scripts to the parent directory one by one. I want to just make a soft symbolic link of the child_directory/script.py
file in parent_directory
. From within the parent_directory
I make the link using ln -s child_directory/script.py .
or ln -s absolute/path/to/child_directory/script.py .
. And then use python script.py
to run the script.
The following is an example code of script.py
:
from utils.py import results_reader
# Do work
When I run the script using the symbolic link in the parent_directory
I get the following error:
ModuleNotFoundError: No module named 'utils'
I don't quite understand what's happening here. os.getcwd()
shows the current path to be the parent_directory/
. Shouldn't it be able to read the utils.py
file? When I run script.py
by copying it to parent_directory/
it runs without errors
And when I change from utils.py import results_reader
to from .utils.py import results_reader
, I get the following error:
ModuleNotFoundError: No module named '__main__.utils'; '__main__' is not a package