#๐Ÿ”’ Imports

26 messages ยท Page 1 of 1 (latest)

alpine zodiac
#

Let's say we have the following file system:

  • Root
  • A
    • a.py
    • b.py
    • c.py
  • main.py

where b.py and c.py use a.py under their own namespaces, and main.py uses everything in A under the A namespace.

How would someone use import in each file for the above to work as mapped?

charred flameBOT
#

@alpine zodiac

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.

dire minnow
alpine zodiac
#

How do you work the __init__.py file?

dire minnow
dire minnow
#

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"

dire minnow
alpine zodiac
#
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'
dire minnow
alpine zodiac
#

from a import * is implicit from startup?

#

So if I started IDLE inside A then it would work?

dire minnow
# alpine zodiac ```py Traceback (most recent call last): File "X:\Root\main.py", line 1, in <m...

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
dire minnow
alpine zodiac
#

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()?

dire minnow
alpine zodiac
#

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.

dire minnow
# alpine zodiac This there a way to iterate through each file in the folder and import them in t...

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

alpine zodiac
#

Thanks.

charred flameBOT
#
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.

#

๐Ÿ”’ Imports