A relative import specifies the resource to be imported relative to the location of the import statement. There are two types of relative imports: implicit and explicit.
Relative imports make use of dot notation to specify location:
- A single dot means that the module or package referenced is in the same directory as the current location.
- Two dots mean that it is in the parent directory of the current location, in other words the directory above.
- Three dots mean that it is in the grandparent directory, and so on.
One clear advantage of relative imports is that they are quite succinct. Unfortunately, relative imports can be messy, particularly for shared projects where directory structure is likely to change. Relative imports are also not as readable as absolute ones, and it’s not easy to tell the location of the imported resources.
bogdanchira on May 15, 2021
How can I import a custom 1.py file (by custom I mean a file I did) into another 2.py file. The 1.py and 2.py are on different partitions on the computer, deep within a tree of folders? 1.py has an object in that helps create certain structures I use in other various projects, so is not part of any project itself (therefore it does not make sense to have it in the project file structure), I just use it to speed up certain things in each project I need it