#๐ Imports
26 messages ยท Page 1 of 1 (latest)
@alpine zodiac
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.
# Root/A/__init__.py
from . import a, b, c
# Root/A/b.py and Root/A/c.py
import a
# Root/main.py
import A
```jez I'm messing all the stuff up today
I tried this:
# Root/A/b.py and Root/A/c.py
from a import *
# Root/main.py
import A
It presumably imports everything, but main.py throws an error saying nothing from A exists.
How do you work the __init__.py file?
that's cause you can't import a folder
if you did import A.a or from A import b, then they'd work
notice the __init__.py I have
basically, think of the __init__.py in A as the script that'll be ran when you do import A
from . import a, b, c means "from the same directory as __init__.py (which is A in this case, which also contains a.py b.py c.py), import a b and c"
so once you have that,
# main.py
import A
# ^ runs __init__.py in A, which imports a, b, c into A
A.b # ok
Traceback (most recent call last):
File "X:\Root\main.py", line 1, in <module>
import A
File "X:\Root\A\__init__.py", line 1, in <module>
from . import a, b, c
File "X:\Root\A\b.py", line 1, in <module>
from a import *
ModuleNotFoundError: No module named 'a'
ah right
replace from a import * with from .a import * (again the . signifies to search in the same dir as that file)
from a import * is implicit from startup?
So if I started IDLE inside A then it would work?
fyi, that error happens because import a and from a import * looks for a in the "current working directory" (cwd), in this case Root is probably your cwd
if you have a terminal that'd be this part
C:\Users\User\Desktop> py main.py
^^^^^^^^^^^^^^^^^^^^^
so you can either leverage "relative imports," i.e. the . thing, or you can do
# Root/A/b.py Root/A/c.py
import a # error, there's no file named `a.py` in `Root/`
import A.a # ok, the file `Root/A/a.py` exists
from A.a import * # ok, same as above
well if you did that, then you won't be able to find main.py
I know.
Seems if I put test functions in each file under a.py, b.py, and c.py naming them after their file and try running it from main.py, I would need to call them as A.a.a(), A.b.b(), and A.c.c(). How do I get it imported so that I call those test functions, for instance, as A.a()?
you can control that in __init__.py, e.g.
# Root/A/__init__.py
from .a import *
from .b import *
from .c import *
Oh.
This there a way to iterate through each file in the folder and import them in that way?
If .a from from .a import * was a string then I would know how, but I don't know what .a is.
personally I've never done it, but look into importlib.import_module to dynamically import some module by their path, and use something like pathlib.Path.glob to find the file paths in a specific folder
Thanks.
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.
๐ Imports