Import Script from a Parent Directory
python import from sibling directory
attempted relative import beyond top-level package
import function from parent directory python
importerror: attempted relative import with no known parent package
python 3 import from parent directory
jupyter notebook import from parent directory
python import from another directory
How do I import a module(python file) that resides in the parent directory?
Both directories have a
__init__.py file in them but I still cannot import a file from the parent directory?
In this folder layout, Script B is attempting to import Script A:
Folder A: __init__.py Script A: Folder B: __init__.py Script B(attempting to import Script A)
The following code in Script B doesn't work:
import ../scriptA.py # I get a compile error saying the "." is invalid
You don't import scripts in Python you import modules. Some python modules are also scripts that you can run directly (they do some useful work at a module-level).
In general it is preferable to use absolute imports rather than relative imports.
toplevel_package/ ├── __init__.py ├── moduleA.py └── subpackage ├── __init__.py └── moduleB.py
from toplevel_package import moduleA
If you'd like to run
moduleB.py as a script then make sure that parent directory for
toplevel_package is in your
Importing modules from parent folder, The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that To import script from a parent directory if you want to run the script directly, you can follow the steps mentioned below:-Add the folder's path to the environment variable (PYTHONPATH). Add the path to sys.path in your script. Then: import module_you_wanted
From the docs:
from .. import scriptA
You can do this in packages, but not in scripts you run directly. From the link above:
Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application should always use absolute imports.
If you create a script that imports A.B.B, you won't receive the ValueError.
Import Script from a Parent Directory, You shouldn't import scripts in Python but, you can import modules. Some python modules are also scripts that you can run directly (they do You cannot import things from parent/sibling directories as such. You can only import things from directories on the system path, or the current directory, or subdirectories within a package. Since you have no __init__.py files, your files do not form a package, and you can only import them by placing them on the system path.
If you want to run the script directly, you can:
- Add the FolderA's path to the environment variable (
- Add the path to
sys.pathin the your script.
Import modules from parent folder in Python · GitHub, import-package-from parent.py. # From http://stackoverflow.com/a/11158224. # Solution A - If the script importing the module is in a package. from .. import In order to import a module or package from a directory that is not in the same directory as the script we are writing (or the directory from which we run the Python interactive interpreter), that module needs to be in a package. As defined above, any directory with a file named __init__.pyis a Python package. This file can be empty.
Import Script from a Parent Directory, Both directories have a __init__.py file in them but I still cannot import a file from the parent directory? In this folder layout, Script B is attempting to import Script A: import importlib scriptName = 'Snake' script = importlib.import_module('Scripts\\.%s' % scriptName) This example has a main.py which is the above code then a folder called Scripts and then you can call whatever you need from this folder by changing the scriptName variable.
The Definitive Guide to Python import Statements, Converting a folder of scripts into an importable package of modules; Running could change (take 2); Case 4: Importing from Parent Directory. The most Pythonic way to import classes from other directories is by using packages. Inside our directory we have two additional subdirectories: air and water. Inside the first directory, there is file plane.py with the class Plane. Inside the directory, there is an __init__.py file.
Python: Import module from parent directory – Code Olives, Suppose this is the directory structure of a project: Our script to run is run.py , which will reference common/a.py . a Imagine we have a PowerShell script which calls another script. For an instance we have two scripts: Scripts\DeploySymbols.ps1 Scripts\IndexSymbols.ps1 And the first one calls the second one as: & .IndexSymbols.ps1. Will it work? Only while we’re calling DeploySymbols.ps1 localy (from “Script” directory): powershell.exe .DeploySymbols.ps1
- Not exactly answering your question, but if you run init.py inside Folder A and try and import Folder B or Script B, Script A will be successfully imported inside Script B.
- Possible duplicate of Importing modules from parent folder
- And why shouldn't sys path "hacks" be used in this case? Python makes it so hard to do what you want here without them. What exactly is the downside here?
- @BT «
sys.path.append(path_to_parent)» shouldn't be an answer to how to fix «"import ../scriptA.py # I get a compile error saying the "." is invalid"» question. There are cases where changing
sys.pathcould be useful e.g., if python itself'd done it or a 3-party module that handles all corner cases correctly done it e.g.,
import autopath; autopath.add_toplevel_to_syspath()that automatically adds parent directory of toplevel_package to
sys.pathto allow a direct internal module execution as a script (or in a REPL) from any directory without proper PYTHONPATH or (virtualenv) install.
- @J.F.Sebastian Is there some kind of proposal for a feature that allows us to explicit say what is the main folder or package of the whole project so that we don't have to care about adding parent directories to
sys.pathso that we can run submodules also as main files or scripts? I have a project where I really need to run scripts both as main or as modules, that is imported, but I need to do tons of hacks adding paths to
sys.pathto make them work in both cases. Would work in a virtualenv or using setuptools work somehow? I am really struggling with this...
- @nbro the feature is called
pip install main-package. You can run "submodules" already (just use their absolute names e.g.,
python -ma.b.c). If it is unclear; ask
- This solution doesn't work for me. It raises: "ImportError: No module named toplevel_package"
- ValueError: Attempted relative import in non-package
- @jgritty, that's because you're doing it in a script that you're running directly.
- Yes, you're right. It will work if you call a script that then imports Script B.
sys.path.append("..")is what you're looking for
- Finally, 6 years later, the code snippet I've been looking for!