If you'd like your script to be more portable, consider finding the parent directory automatically:
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# import ../db.py
import db
Answer from user4490090 on Stack OverflowIf you'd like your script to be more portable, consider finding the parent directory automatically:
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# import ../db.py
import db
You must add the application dir to your path:
import sys
sys.path.append("/path/to/dir")
from app import object
Or from shell:
setenv PYTHONPATH $PYTHONPATH:"path/to/dir"
In case you use Windows: Adding variable to path in Windows.
Or from the command line:
set PYTHONPATH=%PYTHONPATH%;C:\path\to\dir
Import from parent directory
python - Import Script from a Parent Directory - Stack Overflow
importing from parent directory
python - Import from sibling directory - Stack Overflow
Where can I find more Python practice projects?
What Python version does this tutorial target?
How do I install Python for this tutorial?
You could use relative imports (Python >= 2.5):
from ... import nib
(Whatβs New in Python 2.5) PEP 328: Absolute and Relative Imports
I posted a similar answer also to the question regarding imports from sibling packages. You can see it here.
Solution without sys.path hacks
Summary
- Wrap the code into one folder (e.g.
packaged_stuff) - Create a
pyproject.toml(older alternative:setup.py) - Pip install the package in editable state with
pip install -e <myproject_folder> - Import using
from packaged_stuff.modulename import function_name
Setup
I assume the same folder structure as in the question
.
βββ ptdraft
βββ __init__.py
βββ nib.py
βββ simulations
βββ __init__.py
βββ life
βββ __init__.py
βββ life.py
I call the . the root folder, and in my case it is located in C:\tmp\test_imports.
Steps
1) Add a pyproject.toml to the root folder
The contents of the pyproject.toml can be simply
[project]
name = "ptdraft"
version = "0.1.0"
description = "My small project"
[build-system]
build-backend = "flit_core.buildapi"
requires = ["flit_core >=3.2,<4"]
Basically "any" valid pyproject.toml would work. This is just a minimal working example, which uses flit as build backend.
2) Use a virtual environment
If you are familiar with virtual environments, activate one, and skip to the next step. Usage of virtual environments are not absolutely required, but they will really help you out in the long run (when you have more than 1 project ongoing..). The most basic steps are (run in the root folder)
- Create virtual env
python -m venv venv
- Activate virtual env
. venv/bin/activate(Linux) or./venv/Scripts/activate(Win)
- Deactivate virtual env
deactivate(Linux)
To learn more about this, just Google out "python virtualenv tutorial" or similar. You probably never need any other commands than creating, activating and deactivating.
Once you have made and activated a virtual environment, your console should give the name of the virtual environment in parenthesis
PS C:\tmp\test_imports> python -m venv venv
PS C:\tmp\test_imports> .\venv\Scripts\activate
(venv) PS C:\tmp\test_imports>
3) pip install your project in editable state
Install your top level package (here ptdraft) using pip. The trick is to use the -e flag when doing the install. This way it is installed in an editable state, and all the edits made to the .py files will be automatically included in the installed package. Note that the -e flag with pyproject.toml requires pip 21.3 or newer.
In the root directory, run
pip install -e . (note the dot, it stands for "current directory")
You can also see that it is installed by using pip freeze
(venv) PS C:\tmp\test_imports> pip install -e .
Obtaining file:///home/user/projects/ptdraft
Installing build dependencies ... done
Checking if build backend supports build_editable ... done
Getting requirements to build editable ... done
Preparing editable metadata (pyproject.toml) ... done
....
Successfully built ptdraft
Installing collected packages: ptdraft
Successfully installed ptdraft-0.1.0
(venv) PS C:\tmp\test_imports> pip freeze
ptdraft==0.1.0
4) Import by prepending mainfolder to every import
In this example, the mainfolder would be ptdraft. This has the advantage that you will not run into name collisions with other module names (from python standard library or 3rd party modules).
Example Usage
nib.py
def function_from_nib():
print('I am the return value from function_from_nib!')
life.py
from ptdraft.nib import function_from_nib
if __name__ == '__main__':
function_from_nib()
Running life.py
(venv) PS C:\tmp\test_imports> python .\ptdraft\simulations\life\life.py
I am the return value from function_from_nib!
Hello there :)
Assume, you have the following file structure:
.
βββ a
βββ a_file.py
βββ b
βββ b_file.py
In b_file, I want to import functions from a_file. Even adding __init__.py to a/ did not help.
When searching for the problem, I found that I seemingly had to add b/ to PYTHONPATH or whatever. Please tell me there is a more convenient solution.
(If there's not a nice and simple solution, I'd be really interested in why it is not possible to simply import from a parent folder while it is no problem to import from a child directory.)
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
In moduleB:
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 sys.path.
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.
This question has been asked so many times but I'm still not finding what I need. If I call `./run.py` everything works fine. Calling `./foo.py` results in ImportError: attempted relative import with no known parent package. The only way I've found to accomplish this is to use sys.path.append which ends up making a mess of my imports. I don't think I want to use setup tools. Is there not some way to just do an absolute import from "package"? e.g. foo.py would have `import package.l1_dir.bar`
package
|-> run.py
|-> L1_dir
|-> __init__.py
|-> bar.py
|-> L2_dir
|-> __init__.py
|-> foo.py Foo.py
from ..bar import * print("hello from foo.py") Bar.py
print("hello from bar.py") Run.py
from .L1_dir.L2_dir.foo import *
as a literal answer to the question 'Python Import from parent directory':
to import 'mymodule' that is in the parent directory of your current module:
import os
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
os.sys.path.insert(0,parentdir)
import mymodule
edit
Unfortunately, the __file__ attribute is not always set.
A more secure way to get the parentdir is through the inspect module:
import inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
You can use relative import (example from link, current module - A.B.C):
from . import D # Imports A.B.D
from .. import E # Imports A.E
from ..F import G # Imports A.F.G