#πŸ”’ How do packages work?

89 messages Β· Page 1 of 1 (latest)

safe sapphire
#

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?

soft orioleBOT
#

@safe sapphire

Python help channel opened

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.

dawn portal
#

import ..my_project

#

probably you need an init.py in sample/ too for that

safe sapphire
#

Syntax Error

safe sapphire
#

think of my_project as a library

dawn portal
#

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

safe sapphire
#

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)

dawn portal
#

It depends from where you are running your code

#

main
- my_project
-- sample

invoke sample/main.py from main dir

safe sapphire
#

It gives the same error when running from the parent directory of sample and my_project, as well as if I run from sample

dawn portal
#

well actually, if sample contains what its name is you should use this layout instead

root
- my_project
-- init.py
- examples
-- main.py

safe sapphire
#

got it

#

I'll give it a try

dawn portal
#

root is where your terminal should be pointed to

safe sapphire
dawn portal
#

oh i forgot

#

u need to perform an editable install of my_project

safe sapphire
#

πŸ‘€ whats that

dawn portal
#

you say my project is your lib?

safe sapphire
#

we can treat as a lib yes

dawn portal
#

do you have a pyproject.toml in your root/

safe sapphire
#

I do

dawn portal
#

it will declare project metadata

#

send it

safe sapphire
#

sending

dawn portal
#

as text pls

safe sapphire
#
[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

dawn portal
#

so now do pip install -e . from root/

safe sapphire
#

alright

dawn portal
#

and don't put an init.py in examples

safe sapphire
dawn portal
#

You don't need

safe sapphire
#
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.
dawn portal
#

Yea you need to add a few more lines to your pyproject

safe sapphire
#

removing -e from the command seemed to fix it lol

dawn portal
#

please look at setuptools docs i m too lazy to type

dawn portal
safe sapphire
#

oh

dawn portal
#

how did it work?

safe sapphire
#

I'm just taking your word for it

#

alright I'll check out the setuptools docs

dawn portal
#

there's a separate section for pyproject.toml config in setuptools docs

#

last time I checked

lavish falcon
#

Run it like python -m main.sample.main

#

Than u can do both main.myproject or ..myproject

safe sapphire
dawn portal
#

an empty one?

safe sapphire
#

It did call my package "UNKNOWN" though

safe sapphire
dawn portal
#

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

safe sapphire
#

I did this ```toml
[build-system]
requires = [
"setuptools >= 40.9.0"
]
build-backend = "setuptools.build_meta"

dawn portal
#

Add wheel as well to requires

safe sapphire
#

which version

dawn portal
#

nothing

#

Just "wheel"

safe sapphire
#

alright

dawn portal
#

Wheels are good

dawn portal
#

Show your pyproject.toml

safe sapphire
#
[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"
dawn portal
#

Try putting build-system on top

#

Also is your setuptools uptodate?

safe sapphire
#

moved to top

#

same error

dawn portal
#

Is your [project] name and the folder_name same?

safe sapphire
dawn portal
#

Is your pip updated?

safe sapphire
#

I'll upgrade rn

#

updated and still the same error lol

dawn portal
#

This shouldn't happen

safe sapphire
#

i agree

dawn portal
#

I made you do all this because it would help you later anyways

#

Are you using a venv?

safe sapphire
#

i do have python 3.10 that might be an issue

safe sapphire
dawn portal
#

venv or no venv shouldn't be an issue either

glass abyss
safe sapphire
#

!close

soft orioleBOT
#
Python help channel closed

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.