#๐Ÿ”’ relative import

56 messages ยท Page 1 of 1 (latest)

silk phoenix
#

i get module not found when douing this:
importlib.import_module('animedrive_search.py',package='C:\\Users\\vajkh\\OneDrive\\Desktop\\python\\animehun\\src\\provider\\search\\search.py\\..\\search_funcs')

gray kilnBOT
#

@silk phoenix

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.

random bridge
#

It's not relative like that

#

You need to not pass the .py

silk phoenix
#

it has the .py

random bridge
random bridge
#

importlib.import_module(".animedrive_search", package=__package__)

#

I think

silk phoenix
#

whats the difference between useing __package__ or the folder the module is in?

random bridge
#

It needs to be a dotted path

silk phoenix
#

__package__ seems to be an empty string so ill try replaceing the \ with .

random bridge
#

Where are you running the code?

silk phoenix
#

oh wait i am probably runing it from the workspace so src

#

i have to set the paths from there right?

random bridge
#

You probably want to pass importlib.import_module(f"provider.search.search_funcs.{mod_name}")

silk phoenix
#

the path i provided was an absolute path aka from C:

random bridge
#

Yeah no it needs a module name

silk phoenix
#

well i got that it seems but i had to add the path from the search folder that the code is in

#

also i have a few problems with actual relative imports i need to import the pieces module in all of the search funcs and Anilist.info into most of them

random bridge
#

It's always in search_funcs?

silk phoenix
#

thats the dir for that type of extensions

random bridge
silk phoenix
#

can i do something like put src on path localy somehow and add the path from there?

random bridge
#

No don't do that

#

It doesn't help with relative imports anyway

silk phoenix
#

i may not get what a relative import is aint it when u do something like ...Anilist in my case?

#

cus than if i dont go back 2 just go from src that would solve my import error wouldnt it?

random bridge
#

How are you running your code?

#

It's bad form to treat src as a python package

silk phoenix
#

oh yes i forgot to put my modules in an other package as u earlyer suggested but i mean the package that the project is in

random bridge
#

That's a different kind of package I usually call that the dist or distribution

silk phoenix
#

now its animehun

silk phoenix
random bridge
#

Now how do you run your code?

silk phoenix
#

i got to a file and press f5

#

vscode seems to use this command
c:; cd 'c:\Users\vajkh\OneDrive\Desktop\python\animehun'; & 'c:\Program Files\Python311\python.exe' 'c:\Users\vajkh.vscode\extensions\ms-python.debugpy-2024.2.0-win32-x64\bundled\libs\debugpy\adapter/../..\debugpy\launcher' '58585' '--' 'C:\Users\vajkh\OneDrive\Desktop\python\animehun\src\animehun\Anilist\Anilist_flask_server.py'

random bridge
#

You'll need to set a run config with python -m animehun.Anilist.Anilist_flask_server

silk phoenix
#

how do i do that what is that

#

its only a few months that i make code more than 1 file so i dont know much abaut it

random bridge
#

They make it unnecessarily hard

silk phoenix
#

where is that launch.json?

#

i have to go eat ill prob be back in litle less than 1h

random bridge
silk phoenix
#

so i need to set the launch.json to:

    "configurations": [
    {
        "name": "anilist_flask",
        "type": "python",
        "request": "launch",
        "module": "animehun.Anilist.Anilist_flask_server"
    }
]

right?
and what does that do?

#

i get error Error while finding module specification for 'animehun.Anilist.Anilist_flask_server' (ModuleNotFoundError: No module named 'animehun')

random bridge
#

Hmmm you probably need a pip install -e .

silk phoenix
#

whats that?

#

i have no enviroments or anithing

random bridge
#

If you're using a src layout you'll need a pyproject.toml or setup.py to put your src on the path

silk phoenix
#

ill look into that or make a help post tomorrow but i have to go to sleep bye

gray kilnBOT
#
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.