Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

path syntax in python on windows for imports

I have structured my python project on Windows so that Juypter notebooks are in there own folder.

The paths are like this:

\src\tracker\timer.py
\notebooks\notebook_files.nypyb

My import statement in the notebook_file used to look like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from timer import Timer

But now this fails with import could not be resolved.

I have tried various combinations to pass the full path (up one folder and down /src/tracker)

from ../src/tracker.timer import Timer
from ..src/tracker.timer import Timer

But neither of these work. How can I get the import to be resolved?

>Solution :

import statements do not refer to directory paths, but module paths. If you add \src to your PYTHONPATH (or sys.path) you can from tracker.timer import Timer (assuming, that is, that \src\tracker\__init__.py exists so tracker is a valid package)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading