Questions tagged [relative-import]
Relative imports in Python allow you to traverse the packages and modules relative to the script's location.
relative-import
182
questions
1794
votes
15
answers
658k
views
Relative imports for the billionth time
I've been here:
PEP 328 – Imports: Multi-Line and Absolute/Relative
Modules, Packages
Python packages: relative imports
Python relative import example code does not work
Relative imports in Python 2....
438
votes
19
answers
1.1m
views
Relative imports - ModuleNotFoundError: No module named x
This is the first time I've really sat down and tried python 3, and seem to be failing miserably. I have the following two files:
test.py
config.py
config.py has a few functions defined in it as ...
244
votes
13
answers
215k
views
Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3 [duplicate]
I have a directory structure similar to the following
meta_project
project1
__init__.py
lib
module.py
__init__.py
notebook_folder
notebook....
88
votes
1
answer
79k
views
Python relative-import script two levels up
I've been struggling with imports in my package for the last hour.
I've got a directory structure like so:
main_package
|
| __init__.py
| folder_1
| | __init__.py
| | folder_2
| | | ...
31
votes
1
answer
33k
views
Python importing a module from a parallel directory
How would I organize my python imports so that I can have a directory like this.
project
| \
| __init__.py
|
src
| \
| __init__.py
| classes.py
|
test
\
__init__.py
...
27
votes
3
answers
13k
views
Pycharm auto relative imports
Whenever you use autoimport provided by PyCharm it generates an absolute path import. i.e.
from my_package.my_subpackage import my_thing
instead of
from .my_subpackage import my_thing
Is there a ...
26
votes
2
answers
57k
views
Python error - ImportError: attempted relative import with no known parent package [duplicate]
So, my files/folders structure is the following:
project/
├─ utils/
│ ├─ module.py
├─ server/
│ ├─ main.py
Inside project/server/main.py I'm trying to import project/utils/module.py using this ...
26
votes
2
answers
70k
views
Attempted relative import beyond toplevel package
Here is my folder structure:
Mopy/ # no init.py !
bash/
__init__.py
bash.py # <--- Edit: yep there is such a module too
bass.py
bosh/
__init__.py # contains from .. ...
15
votes
1
answer
4k
views
Relative import error with py2exe
I was trying to generate an executable for a simple Python script. My setup.py code looks like this:
from distutils.core import setup
import py2exe
setup(console=["script.py"])
However, I am getting ...
14
votes
4
answers
9k
views
Python Relative Import in Jupyter Notebook
Let's say I have the following structure:
dir_1
├── functions.py
└── dir_2
└── code.ipynb
In, code.ipynb, I simply want to access a function inside functions.py and tried this:
from .....
14
votes
1
answer
18k
views
How can I fix the relative import error: "with no known parent package"?
I have been trying for a couple of days to solve the relative import error in my project with no success.
I have the following project tree:
proj
|
|---__init__.py
|
|---lib
| |---__init__.py
| |...
12
votes
1
answer
15k
views
relative import from __init__.py file throws error
So I'm using a template set by a co-worker and as a newbie to python I may be missing something very obvious.
The main directory has the init file which has a module I need for the main python file ...
7
votes
1
answer
531
views
What is the correct boilerplate for explicit relative imports?
In PEP 366 - Main module explicit relative imports which introduced the module-scope variable __package__ to allow explicit relative imports in submodules, there is the following excerpt:
When the ...
5
votes
2
answers
4k
views
How to debug a python module that needs to be executed with -m?
Every debugger I tried out there expects a source file to debug. However Python does not always work this way.
I have a module that is a folder with __init__.py and __main__.py files inside, among ...
5
votes
1
answer
2k
views
What does 'from dot import asterisk' do in Python 3?
Question
What does the following line do in Python 3?
>>> from . import *
What I found out so far...
It does not output anything and the only change I can see in Python 3.7.3 is the ...
4
votes
4
answers
12k
views
relative import in python 3.9.5
My folder structure is as follows
./fff
├── __init__.py
├── fg
│ ├── __init__.py
│ └── settings
│ ├── __init__.py
│ └── settings.py
└── obng
└── test.py
I want to import the ...
4
votes
1
answer
2k
views
Location for app-wide utils file in a Django project
Given the nightmare of relative imports in Python, where should I put a simple utils.py file in a Django project that has multiple apps (accessible within the models.py, admin.py, views.py all files ...
4
votes
2
answers
10k
views
Python packages: relative imports
I'm working on a Python application consisting of a core and multiple independent modules using the core. I'm having difficulty setting up relative imports of packages.
app
|- __init__.py
|- core
...
4
votes
1
answer
2k
views
Python project structure and relative imports
I'm new to Python and I searched google a lot and read some articles about relative imports etc. Despite the fact that I am unable to get it working. Please, consider my following project structure:
/...
4
votes
1
answer
295
views
Python relative/absolute import (again)
This topic has been covered several times but I still can't get my package to work.
Here is the situation: I've got a package in which a logging module takes care of setting up the logging.
So clearly,...
3
votes
3
answers
10k
views
Python sibling relative import error: 'no known parent package'
I want to import a module from a subpackage so I went here Relative importing modules from parent folder subfolder and since it was not working I read all the literature here on stack and found a ...
3
votes
1
answer
675
views
Python3: Relative import beyond toplevel [duplicate]
Yes, there are similar questions, but they do not answer my issue. My directory structure is as follows, all __init__.py files are blank.
Package/
__init__.py
sub_package1/
...
3
votes
1
answer
1k
views
Google Python Style Guide & relative imports
Google Python Style Guide says:
Do not use relative names in imports. Even if the module is in the same package, use the full package name. This helps prevent unintentionally importing a package ...
3
votes
1
answer
7k
views
Relative import inside project results in "SystemError: Parent module '' not loaded, cannot perform relative import"
I am working on a project which has the following structure:
project
├── config.py
└── modules
└── a.py
According to PEP 328 relative imports are possible.
However when I start Python (in shell)...
3
votes
3
answers
997
views
Python Relative Import From Parent Directories
I've read many docs over the last few days about relative Python imports but run into a struggle with the following folder structure:
parent_folder
├── subfolder1
│ └── __init__....
3
votes
1
answer
151
views
Attempted relative import with no known parent package when doing automated testing
My simplified folder structure is:
projectroot/
__init__.py
src/
__init__.py
util.py
tests/
__init__.py
test_util.py
In util.py I have the following function:
def build_format_string(...
3
votes
1
answer
258
views
Mechanism - why direct run on main with relative import cause "attempted relative import with no known parent package"
Run python main.py where main.py has relative imports will fail.
<root>
└── src
├── main.py # from main.py, want to use the stuff in other.py
└── other.py
$ ...
3
votes
0
answers
140
views
Why is "attempted relative import beyond top-level package" ValueError, not ImportError?
When I do a relative import beyond the top level package, I get an exception — no surprise there. But why is it a ValueError and not an ImportError?
$ find .
.
./b
./b/c.py
./b/__init__.py
./a.py
$ ...
2
votes
1
answer
4k
views
Why Pycharm can't run code with relative imports?
My package structure:
root_pkg
root_pkg/__init__.py # empty
root_pkg/game
root_pkg/game/__init__.py # empty
root_pkg/game/game1.py
root_pkg/utils
root_pkg/utils/__init__.py # empty
root_pkg/utils/lib....
2
votes
1
answer
1k
views
Importing Python class from sibling directory
My Directory structure in /VM/repo/project is:
__init__.py
scripts/
getSomething.py
__init__.py
classes/
project.py
db.py
__init__.py
getSomething.py
from ..classes ...
2
votes
1
answer
592
views
Flask Bug on Python 3.4? Development server can't run if app contains relative imports
By design, Python 3 cannot run a module that contains relative imports as a script. Attempting to do so yields the following error:
$ python mypackage/run.py
[...traceback...]
SystemError: Parent ...
2
votes
2
answers
1k
views
Relative import problem with python packages
First of all, I have look through a lot of question-answer regarding this issue but I couldn't find a solution to my problem.
I am on Python 3.7.9 and my file structure looks as in the following
...
2
votes
2
answers
2k
views
Python Relative Import cannot find package
I'm sure that this is a pretty simple problem and that I am just missing something incredibly obvious, but the answer to this predicament has eluded me for several hours now.
My project directory ...
2
votes
1
answer
68
views
Relative import - from from ZSSGAN.model.ZSSGAN import ZSSGAN
I'm working on the StyleGAN-NADA repo, and I am having issues loading the ZSSGAN function. The command line to do so is from ZSSGAN.model.ZSSGAN import ZSSGAN, where
The first ZSSGAN is the name of a ...
2
votes
2
answers
577
views
ImportError in Python 3.8.6
I am using python 3.8.6 to build a simple web app with the module Flask and the folder structure looks exactly like this:
web-app
├── main.py
└── site
├── __init__.py
├── auth.py
└── ...
2
votes
1
answer
433
views
How to get rid of relative import in Typescript + Node 18?
I'm currently running a backend application in Typescript 5.2, which can be build to run as a Debian service on production with node 18 or directly run in a Docker container through nodemon.
...
2
votes
1
answer
2k
views
Can't resolve react path alias
Edit: I solved it, in the comments.
I'm trying to use alias for paths but I can't seem to get it to work I keep getting this error even though this path is correctly predicted by vs code.
This is a ...
2
votes
1
answer
704
views
How to programmatically add root folder to the path for every script?
The file structure I currently have is something like:
__init__.py
script_folder
__init__.py
s1.py
s2.py
b_folder
__init__.py
b1.py
b2.py
lib_folder
__init__.py
lib1.py
...
2
votes
2
answers
134
views
Failing at importing modules on items.py file - Scrapy
I'm new in Python and I'm currently learning webscraping with spiders. Following the tutorial, I stucked at relative importing with Python.
This is the structure of my current folder (provided by ...
2
votes
1
answer
64
views
Import from parent directory for a test sub-directory without using packaging, Python 2.7
TL;DR
For a fixed and unchangeable non-package directory structure like this:
some_dir/
mod.py
test/
test_mod.py
example_data.txt
what is a non-package way to enable ...
2
votes
0
answers
351
views
How to solve relative import error if occurs in third-party libraries
I'm currently trying to create an executable for my application with Pyinstaller.
I already could fix some general import errors by including the respective libraries in the hiddenimports section in ...
2
votes
0
answers
985
views
Make PyLint and VSCode autocompletion understand relative import
In my Python project I want to import a self-compiled .so-file named example.cpython-38-x86_64-linux.so written with pybind11 and stored in a subdirectory called bin.
To do so I use import bin.example ...
2
votes
1
answer
740
views
Import Error : attempted relative import with no known parent package
Can you please tell me how to do relative import correctly.
Project Structure:
p1
|- x1
| |- __init__.py
| |- x1_module1.py
|- x2
|- __init__.py
|- x2_module1.py
In x2_modules.py
try:
...
2
votes
1
answer
743
views
(Python) relative import best-practice (import x or from . import x) [duplicate]
I'm writing my own module mymodule. It contains this:
myroot
|-mymodule
| |-file1
| |-file2
|
|-tests
| |-...
|
|-...
In file1 I want to import file2. First I used from . import file2 because this ...
1
vote
2
answers
1k
views
Python importing only modules within package
I am creating a Python package with multiple modules. I want to make sure that when I import modules within the package that they are importing only from the package and not something outside the ...
1
vote
2
answers
4k
views
import function from another folder's file
My project directory looks like this:
project/
├─ new/
│ ├─ test.py
├─ docs.py
├─ main.py
Within my main.py, I import a function from docs.pylike this:
from docs import get_my_conn
and it works ...
1
vote
3
answers
2k
views
Import module from directory one level up when running script in subdirectory
Say I have a collection of scripts organized for convenience in a directory structure like so:
root
│ helpers.py
│
├───dir_a
│ data.txt
│ foo.py
│
└───dir_b
data.txt
bar....
1
vote
2
answers
100
views
Relative import between modules
I've written a group of functions that I wanted to use for my computation and I've organize them in some .py file, say functions1.py and functions2.py. Within the same folder I have also another file ...
1
vote
2
answers
766
views
How to import module inside package
Folder structure:
foo/
bar.py
__init__.py
test.py
foo/__init__.py has import bar
I am running test.py which has import foo
How do I get __init__.py to import bar.py?
ModuleNotFoundError: No ...
1
vote
1
answer
582
views
Python 3 "Sideways" Relative Import [duplicate]
I have a file structure like
math/
snippets/
numerical_methods.py
homework1/
main.py
homework2/
main.py
And in homework 1's main.py, I would like to do
from .....