You could use relative imports (Python >= 2.5):
from ... import nib
(Whatโs New in Python 2.5) PEP 328: Absolute and Relative Imports
Answer from f3lix on Stack OverflowHello 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.)
python - Importing modules from parent folder - Stack Overflow
python - Import file from parent directory causes "Import by filename is not supported" - Stack Overflow
How to do absolute import (a parent folder from the child folder)
importing from parent directory
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!
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
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
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 *