0

The following operations is under Win10:

  1. the dictionary structure:
│  LICENSE
│  README.md
│  setup.py
│
└─dlpk
        a.py
        b.py
        __init__.py
  1. the contents of the files:

the __init__.py is empty.

# b.py:
from a import A

# a.py:
class A:
    pass

# setup.py:
import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name="testDL-DeaL",
    version="0.0.9",
    author="Dai Ling",
    author_email="dialing57@163.com",
    description="error report",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/dailing57/DLex",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    packages=setuptools.find_packages(),
    python_requires=">=3.6",
)
  1. Execute following commands:
python setup.py sdist
twine upload --repository testpypi dist/*
  1. Import it from the test.pypi create and activate the venv

do

pip install --index-url https://test.pypi.org/simple/ --no-deps testDL-DeaL==0.0.9

write a test file: test.py:

from dlpk import b

and run it, it shows:

Traceback (most recent call last):
  File "H:\DLang\TEST\test.py", line 1, in <module>
    from dlpk import b
  File "H:\DLang\TEST\venv\lib\site-packages\dlpk\b.py", line 1, in <module>
    from a import A
ModuleNotFoundError: No module named 'a'

How does this error happen?

Dai
  • 49
  • 5
  • As per the linked questions, the statement `from a import A` must be changed to either `from dlpk.a import A` (absolute import) or `from .a import A` (relative import). – metatoaster Mar 21 '22 at 04:46
  • but it will report `ModuleNotFoundError: No module named 'dlpk' ` for `from dlpk.a import A` and `ImportError: attempted relative import with no known parent package` for `from .a import A` – Dai Mar 21 '22 at 04:52
  • That second error means you have not install the package into the currently active Python environment. – metatoaster Mar 21 '22 at 04:53
  • I develope it under the global python environment. Does it mean that I should install the developing package to the global environment? – Dai Mar 21 '22 at 11:30
  • The python you are running may be picking up a mix of the installed package and the developed package, possibly because of where your current working directory is. I cannot seem to replicate your issue. Try doing a `import dlpk` and then `print(dlpk)` to verify the thing you are trying to import is in fact the one you are expecting. You should also use a virtual environment while developing to constrain the issue (and make it easier for you to isolate the issue which you can do by creating a new one and optionally deleting the old one). – metatoaster Mar 22 '22 at 03:16
  • What I did was I created a new virtualenv, activated it, pip install the test package you uploaded to the test PyPI, verified that `python -c 'from dlpk import b'` failed, modified the installed `b.py` file's import statement to one of my suggestions, either one worked. – metatoaster Mar 22 '22 at 03:18

0 Answers0