How to Import a Python Class That Is in a Directory Above

How to import a Python class that is in a directory above?

from ..subpkg2 import mod

Per the Python docs: When inside a package hierarchy, use two dots, as the import statement doc says:

When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification for relative imports is contained within PEP 328.

PEP 328 deals with absolute/relative imports.

Importing modules from parent folder

It seems that the problem is not related to the module being in a parent directory or anything like that.

You need to add the directory that contains ptdraft to PYTHONPATH

You said that import nib worked with you, that probably means that you added ptdraft itself (not its parent) to PYTHONPATH.

How to import the class within the same directory or sub directory?

Python 2

Make an empty file called __init__.py in the same directory as the files. That will signify to Python that it's "ok to import from this directory".

Then just do...

from user import User
from dir import Dir

The same holds true if the files are in a subdirectory - put an __init__.py in the subdirectory as well, and then use regular import statements, with dot notation. For each level of directory, you need to add to the import path.

bin/
main.py
classes/
user.py
dir.py

So if the directory was named "classes", then you'd do this:

from classes.user import User
from classes.dir import Dir

Python 3

Same as previous, but prefix the module name with a . if not using a subdirectory:

from .user import User
from .dir import Dir

Trying to import classes from parent folder as a package

If you run python test1.py and it works, that implies (unless you used some other technique to set up the sys.path) you are in the init_file_testing folder, and that folder is on the path that is searched for Python modules. If you want to from subdir import something, then you need that folder on your path - because that is the folder that contains the subdir package.

If you run python test2.py and the file is found, that implies (similar logic) you are in subdir2. Hacking the path with sys.path.append("..") does not help, because that puts the subdir folder on the path, but you need the init_file_testing folder instead. In relative terms, that is sys.path.append("../..").


However, if you are willing to use relative paths as part of a hack to make this work, then why not just use relative imports instead? This is how you are intended to work within packages.

That looks like:

test1.py

from .subdir import Apple, Banana

__init__.py

from .class1 import Apple
from .class2 import Banana

test2.py

from ..subdir import Apple, Banana

Then, you only need to ensure that the package root (init_file_testing) is on the module search path, no matter which module you start from; and that you start from either the root folder or from outside the package folders (which you should do anyway). One easy way to ensure the path is set up is by installing your package in a virtual environment (which is the recommended approach for development anyway). You can also do it with the PYTHONPATH environment variable.

Import a class from different folder levels - Python

Within the same package you can do relative import but since you are going out of your current package, you need to do absolute imports.

from MyProject.models.Encoder import BiDirectionalEncoder 

Can't get Python to import from a different folder

I believe you need to create a file called __init__.py in the Models directory so that python treats it as a module.

Then you can do:

from Models.user import User

You can include code in the __init__.py (for instance initialization code that a few different classes need) or leave it blank. But it must be there.

Python Imports From The Directory Above

import sys
sys.path.append('/your/dir/goes/here')
from base import foo

Something like that should permit you to import stuff from any directory of your choosing.

Not able to import a python class from another directory in python

There are multiple ways to work around this. First way is to move the supervised folder containing source.py into the scripts folder. So the file structure would look something like this:-

MLJAR-SUPERVISED
├── examples
│── notebooks
|── scripts
|── target.py
└── supervised
└── source.py

The second way is extensively answered here...link



Related Topics



Leave a reply



Submit