1

I created a package like this:

└─dd
    a.py
    b.py
    __init__.py

a.py and __init__.py is empty.

b.py is:

from dd import a

When I run the b.py, I get the error message:

from dd import a ModuleNotFoundError: No module named 'dd'

Why the dd package can't be recognized ?

UPDATE1

The reason I do this is that after I published my package to PyPl, and then, I imported it, but it reports the error that it can't recognize the module which is in the same package.

For example, if I do it like this:

# b.py
import a

then publish the dd package to PyPl

pip install dd

If I try from dd import b, it will report the error that it doesn't kown what is a

So, how to solve this problem ?

Dai
  • 49
  • 5
  • That import statement expects `dd` to be found in the current directory. But you're _already_ in the `dd` directory. – John Gordon Mar 20 '22 at 18:01
  • 2
    You created a file hierarchy. How that gets turned into a package is another matter, one that depends on how exactly you are executing `b.py` and how (if at all) your code is being installed. – chepner Mar 20 '22 at 18:02
  • In general, though, a directory can be treated as a package if it contains `__init__.py` and it appears in a directory on your search path. Whether you need `__init__.py` depends on whether you installed the code as a namespace package. – chepner Mar 20 '22 at 18:03

1 Answers1

0

You should do this inside b.py:

import a

# or
from a import *
Mohammad Jafari
  • 1,742
  • 13
  • 17
  • 2
    This is an option, but it's not the only option, or even the _best-practice_ option (and wildcard imports are outright considered an antipattern). Look up "absolute import". Granted, doing it right might mean expanding the scope of the answer to make sure the OP is installing their module correctly, or otherwise getting a PYTHONPATH that includes it. – Charles Duffy Mar 20 '22 at 18:06