1

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?

  • Try `python -m pip install ethlib`. Most likely `pip` and `python` don't belong to the same installation. – a_guest Jul 23 '21 at 23:00
  • Tried, same behaviour – Alberto Pirillo Jul 23 '21 at 23:12
  • What is the name of your project? There is no project named [`ethlib` on pypi](https://pypi.org/project/ethlib/). Also could you share the content of your `setup.py` or `pyproject.toml` files? – a_guest Jul 25 '21 at 11:56
  • That is because I deleted the package. I am really fed up with how python works with imports and modules atm, but I will try again someday – Alberto Pirillo Jul 25 '21 at 21:54
  • 1
    You should really share the content of your `setup.py` file. My feeling is that you have something like `setup(name='ethlib', packages=find_packages(), ...)`, and then on your local disk the actual folder containing the package is named differently than `ethlib`. So if you did for example `setup(name='ethlib', packages=['foo'], ...)`, then `ethlib` is the name under which the *project* will be identified on PyPI but `foo` is the actual *package* that it contains (so you'd need to `import foo`). Please refer to [this answer](https://stackoverflow.com/a/62951928/3767239) for more information. – a_guest Jul 26 '21 at 06:52
  • @a_guest thanks for your help man, let's give it a try. I updated the question with what you requested (beware that I also changed the package's name) – Alberto Pirillo Jul 26 '21 at 08:01
  • Okay, so apparently you put all `.py` modules inside the same directory where the `setup.py` script is located? Instead, just create a folder next to `setup.py`, called `ethbotutils` and then put all Python files there (except the setup script). Then in `setup.py` you need to specify `packages=["ethbotutils"]`. This tells setuptools to create a *distribution* called `ethbotutils` which contains/ships a *package* of the same name which you can then import; i.e. `import ethbotutils` or `from ethbotutils.bot_utils import xyz`. – a_guest Jul 26 '21 at 08:14
  • Many thanks, not it works flawlessly. If you make an answer with a recap I will mark it as accepted. – Alberto Pirillo Jul 26 '21 at 09:18

1 Answers1

1

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 (e.g. 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; i.e. 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.

a_guest
  • 34,165
  • 12
  • 64
  • 118