Have been super confused about how packages worked for a while, along with relative imports and things along those lines.
Let's say I have code structured like this: |- sample/ | | - main.py |- my_project/ | |- data/ | |- src/ | |- __init__.py Why can I not do import my_project from sample/main.py? What would I need to change to do that?
#π How do packages work?
89 messages Β· Page 1 of 1 (latest)
@safe sapphire
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
Syntax Error
odd
think of my_project as a library
For things to get imported correctly u need to have a root dir with init.py. Import relative to root dir in all sub dirs and modules
The issue I'm facing right now is if I put an import my_project in sample/main.py, even if the __init__.py in my_project is empty, I wouldn't expect it to give an import error (module not found error)
It depends from where you are running your code
main
- my_project
-- sample
invoke sample/main.py from main dir
It gives the same error when running from the parent directory of sample and my_project, as well as if I run from sample
root is where your terminal should be pointed to
looks identical to my current file structure
π whats that
you say my project is your lib?
we can treat as a lib yes
do you have a pyproject.toml in your root/
I do
sending
as text pls
[project]
name = "---"
version = "0.0.1"
dependencies = [
"requests >= 2.31.0",
"toml >= 0.10.2",
"numpy >= 1.26.4",
"beautifulsoup4 >= 4.12.3"
]
requires-python = ">= 3.10"
authors = [
{ name = "Tathya Garg", email = "-----" }
]
description = "------"
readme = {file = "README.md", content-type = "text/markdown"}
license = { file = "LICENSE"}
[project.urls]
Homepage = "-----"
Repository = "----"
oops haha
so now do pip install -e . from root/
alright
You don't need
ERROR: Project ---- has a 'pyproject.toml' and its build backend is missing the 'build_editable' hook. Since it does not have a 'setup.py' nor a 'setup.cfg', it cannot be installed in editable mode. Consider using a build backend that supports PEP 660.
Yea you need to add a few more lines to your pyproject
removing -e from the command seemed to fix it lol
please look at setuptools docs i m too lazy to type
oh
how did it work?
there's a separate section for pyproject.toml config in setuptools docs
last time I checked
Run it like python -m main.sample.main
Than u can do both main.myproject or ..myproject
well I added a setup.cfg file and ran pip install -e . as sudo and it worked
an empty one?
It did call my package "UNKNOWN" though
yea
That works because that's a fallback behavior as of now
You should consider adding the [build-system] section in your pyproject.toml and get rid of setup.cfg
I did this ```toml
[build-system]
requires = [
"setuptools >= 40.9.0"
]
build-backend = "setuptools.build_meta"
Add wheel as well to requires
which version
alright
Wheels are good
same error
Show your pyproject.toml
[project]
name = "---"
version = "1.0.0"
dependencies = [
"requests >= 2.31.0",
"toml >= 0.10.2",
"numpy >= 1.26.4",
"beautifulsoup4 >= 4.12.3"
]
requires-python = ">= 3.10"
authors = [
{ name = "Tathya Garg", email = "---" }
]
description = "---"
readme = {file = "README.md", content-type = "text/markdown"}
license = { file = "LICENSE"}
[project.urls]
Homepage = "---"
Repository = "---"
[build-system]
requires = [
"setuptools >= 40.9.0",
"wheel"
]
build-backend = "setuptools.build_meta"
updated just now
moved to top
same error
in our example the name would be my_project here
Is your pip updated?
i agree
I made you do all this because it would help you later anyways
Are you using a venv?
i do have python 3.10 that might be an issue
nope
venv or no venv shouldn't be an issue either
nah
you simply cant do it. its not in the scope of main.py. if you wanted to do that. you would have to enclose both the folders within another packages suppose my_package. Within that package, you would need an empty __init__.py. basically __init__.py is what makes a folder a package. once you have that setup, you can import using import ..my_project
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
