Skip to content Skip to sidebar Skip to footer

Load Py File From Path/folder

I busy with some calculations in Python and therefore i have some bunch of scripts. I have tried to clean this up thru 2 folders named scripts and tests. Now i have the problem tha

Solution 1:

You can do relative imports from where you are. Let's assume you're importing from the file /home/janbert/projects/test/test.py

If you want to import /home/janbert/projects/test/subdir/file.py you write:

from subdir import file

And if you want to import /home/janbert/projects/otherproject/subdir/file.py you write:

from ..otherproject.subdirimport file

Just remember that each python package (ie folder) must have a file named __init__.py (which can be empty), otherwise you can not import from that package.

Post a Comment for "Load Py File From Path/folder"