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 executefrom . import mod
from a module in thepkg
package then you will end up importingpkg.mod
. If you executefrom ..subpkg2 import mod
from withinpkg.subpkg1
you will importpkg.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
Why Is Numpy's Einsum Faster Than Numpy's Built in Functions
List Running Processes on 64-Bit Windows
Python Locale Error: Unsupported Locale Setting
Django: How to Serve Media/Stylesheets and Link to Them Within Templates
I Can't Import Python Modules in Xcode 11 Using Pythonkit
Install Rpy2 on Windows7 64Bit for Python 2.7
Is There a Static Analysis Tool for Python, Ruby, SQL, Cobol, Perl, and Pl/Sql
What Does Blazeds Livecycle Data Services Do, That Something Like Pyamf or Rubyamf Not Do
Function Which Returns the Least-Squares Solution to a Linear Matrix Equation
How to Take Partial Screenshot with Selenium Webdriver in Python
Pythonic Way of Checking If a Condition Holds for Any Element of a List
Differencebetween List and List[:] in Python
Creating a List of Dictionaries Results in a List of Copies of the Same Dictionary
Python != Operation VS "Is Not"
Matplotlib Does Not Show My Drawings Although I Call Pyplot.Show()
Pandas Get Column Average/Mean
What Is the Good Python3 Equivalent for Auto Tuple Unpacking in Lambda