简体   繁体   中英

ModuleNotFoundError with package from PyPi

I uploaded a package on PyPi using twine, and it went fine. Now I'm trying to install that package and importing it into a script.

According to pip the module is already installed correctly:

PS C:\Users\alber> pip install ethbotutils
Requirement already satisfied: ethbotutils in c:\users\alber\appdata\local\programs\python\python39\lib\site-packages (1.1)

But when I try to import it in a script or in a IDE or in Python IDLE i get:

>>> import ethbotutils
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import ethbotutils
ModuleNotFoundError: No module named 'ethbotutils'

This is the pyproject.toml file (stored in the project root):

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"

And this the setup.py file (stored withing the package directory):

from setuptools import setup

setup(
    name='ethbotutils',
    version=1.0,
    packages=["."],
    install_requires=["requests~=2.25.1", "PyYAML~=5.4.1"],
    python_requires=">=3.6"
)

EDIT:

What @a_guest suggested seems to be working: if I import a script present in the package, like "bot_utils" everything works, but it still doens't when I try to import the whole package by its name. How can I fix that?

The name of a distribution (the name parameter of setup ) determines how a distribution (or project) is identified within the Python ecosystem; this includes the Python Package Index , where the distribution will be located at the URL https://pypi.org/project/<name>/ .

This is distinct from the actual package(s) that a distribution contains (the packages parameter of setup ). It is these packages that are made available when installing a distribution (eg via pip ).

So as an example, if the setup.py file contains the following specification

setup(
    name='foo',
    packages=['bar'],
    ...
)

then this will create a distribution named foo which installs a package named bar ; ie after doing pip install foo , the content of that distribution (the package) can be accessed via import bar . Typically the name of a distribution and the top-level package should coincide in order to avoid conflicts with other distributions which might get installed into the same virtual environment (see this answer for more information).

For the specific example of the OP, this means that the setup.py file should contain the following specification:

setup(
    name='ethbotutils',
    packages=['ethbotutils'],
    ...
)

In order for the setup to work, all relevant Python modules need to be placed inside a local folder ethbotutils which exists next to the setup.py file.

Is your IDE set up to read from the same directory that the library is installed?

Have you run a pip install from inside the IDE?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM