Questions tagged [relative-import]

Relative imports in Python allow you to traverse the packages and modules relative to the script's location.

166 questions
1668
votes
12 answers

Relative imports for the billionth time

I've been here: http://www.python.org/dev/peps/pep-0328/ http://docs.python.org/2/tutorial/modules.html#packages Python packages: relative imports python relative import example code does not work Relative imports in python 2.5 Relative imports in…
user1881400
412
votes
19 answers

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 well as a few variables. I've stripped it down to…
blitzmann
  • 7,319
  • 5
  • 23
  • 29
237
votes
13 answers

Import local function from a module housed in another directory with relative imports in Jupyter Notebook using Python 3

I have a directory structure similar to the following meta_project project1 __init__.py lib module.py __init__.py notebook_folder notebook.jpynb When working in notebook.jpynb if I try to use…
mpacer
  • 3,009
  • 2
  • 16
  • 15
79
votes
1 answer

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 | | | __init__.py | | | script_a.py | | | script_b.py | …
29
votes
1 answer

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 tests.py And then inside…
Zack
  • 13,454
  • 24
  • 75
  • 113
26
votes
2 answers

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 .. import bass bsa_files.py ... …
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
25
votes
3 answers

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 setting to use relative imports instead of absolute…
Laimonas Sutkus
  • 3,247
  • 2
  • 26
  • 47
20
votes
2 answers

Python error - ImportError: attempted relative import with no known parent package

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 syntax: from ..utils.module import my_function. I'm…
Ricardo Passos
  • 373
  • 1
  • 3
  • 10
15
votes
1 answer

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 the error shown in the screenshot. Is there…
multigoodverse
  • 7,638
  • 19
  • 64
  • 106
14
votes
1 answer

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 | |---func.py | |---src | |---main.py the…
Ahmed Garssallaoui
  • 167
  • 1
  • 3
  • 10
13
votes
4 answers

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 ..functions import some_function I get the…
CHRD
  • 1,917
  • 1
  • 15
  • 38
10
votes
1 answer

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 that generates the error. Co-worker used: from .…
thewellis
  • 175
  • 2
  • 2
  • 6
7
votes
1 answer

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 main module is specified by its filename, then…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
5
votes
2 answers

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 others, and I usually execute that this way: $ cd…
Yajo
  • 5,808
  • 2
  • 30
  • 34
4
votes
4 answers

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 settings.py inside fg/settings as a module into the…
Sharath Prakash
  • 186
  • 3
  • 12
1
2 3
11 12