Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14301

pyproject correct way to import parent

$
0
0

I am trying to follow the correct way to import files in python but I can't make it works.
This is my project :

import_test/├── foo│  ├── bar│  │  ├── bar.py│  │  ├── baz│  │  │  ├── baz.py│  │  │  └── __init__.py│  │  └── __init__.py│  ├── foo.py│  └── __init__.py├── __init__.py├── main.py└── pyproject.toml

what I want : I want to call bar.py from main.py and I want to call main.py from baz.py
how I did it :this is my pyproject :

[project]name = "import_test"version = "0.1.0"description = "testing import"

main.py :

from import_test.bar import barvar_from_main = 0print(bar)

bar.py :

bar = 2

baz.py :

from import_test.main import var_from_mainprint(var_from_main)

this is the result I have :

> python main.pyTraceback (most recent call last):  File "import_test/main.py", line 1, in <module>    from import_test.bar import *    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ModuleNotFoundError: No module named 'import_test'> python foo/bar/baz/baz.pyTraceback (most recent call last):  File "import_test/foo/bar/baz/baz.py", line 1, in <module>    from import_test.main import var_from_mainModuleNotFoundError: No module named 'import_test'

Viewing all articles
Browse latest Browse all 14301

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>