0
├── GitHub
│   └── git_start.py
├── automation
│   ├── Docker
│   │   ├── Dockerfile
│   │   └── requirements.txt
│   ├── main.py
│   ├── modules
│   │   ├── __init__.py
│   │   ├── config.py
│   └── test.py

I want to import git_start module(has it's own configuration) from test.py. I tried everything, always get:

ImportError: attempted relative import with no known parent package
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
S1c0r4x
  • 47
  • 2
  • 6
  • Maybe try to add an `__init__.py` file in the `GitHub` folder to make it a python package. Then, in `test.py`, start with `from .GitHub import git_start` (notice the dot), or `from ..GitHub import git_start` (two dots) – Reblochon Masque Nov 23 '20 at 17:19
  • 1
    @ReblochonMasque: All folders are valid Python packages (specifically, [implicit namespace packages](https://www.python.org/dev/peps/pep-0420/)) in modern Python; you don't need `__init__.py`. Problem is that if your working directory is the parent of both `GitHub` and `automation`, those are unrelated packages; `automation.test` can't use relative imports to get `GitHub.git_start`. If `automation` is the working directory, `GitHub` isn't even a visible package for absolute imports. – ShadowRanger Nov 23 '20 at 17:28
  • Yes @ShadowRanger - maybe copy `git_start.py` to wherever installed packages are kept something like `envs/env/lib/python3.x` ? – Reblochon Masque Nov 23 '20 at 17:40
  • @ReblochonMasque: I mean, the correct solution is to install it properly with an appropriate `setup.py` so absolute imports find it (and don't use relative imports when it's not actually part of the same package). For simple test cases, you might manually modify `sys.path` (or even hackier, tweak the definition of `__package__` on the script), as demonstrated in some of the answers on the duplicate. – ShadowRanger Nov 23 '20 at 17:45

0 Answers0