I have this directory structure
- folder
- measuring_scripts
measure_something.py
- analysis_scripts
plot_measured.py
- measuring_scripts
I want to import a function from plot_measured.py into measure_something.py. So measure_something.py begins like this
PATH_TO_ANALYSIS_SCRIPTS = Path(__file__).resolve().parent.parent/'analysis_scripts'
print(PATH_TO_ANALYSIS_SCRIPTS)
import sys
sys.path.append(PATH_TO_ANALYSIS_SCRIPTS)
from plot_measured import something
# More stuff
This prints
/home/someone/blah/folder/analysis_scripts
Traceback (most recent call last):
File "measure_something.py", line 5, in <module>
from plot_measured import something
ModuleNotFoundError: No module named 'plot_measured'
How can I do this? I tried converting analysis_scripts into a module adding the __init__.py file and changing to from analysis_scripts.plot_measured import something but it is the same.
I know this has been asked a million times. I have myself even done this thousands of times in the past, but cannot see where I am failing this time...
__init__.pyfile in each folder and doingfrom ...analysis_scripts.plot_measured import somethingproducesImportError: attempted relative import with no known parent package.foldershould be one package. But then, you should move the scripts outside of the package. If you have code common to both scripts, move the code into its own package, and let both scripts import it. It does require some restructuring and rewriting, but it may benefit you in the long term.Pathobject has to be casted tostr.