1

For many python libraries, the argument used with import is the same as the one used to install the library with pip.

E.g.

pip install numpy
pip install scipy
pip install pandas

correspond to

import numpy
import scipy
import pandas

but this pattern doesn't seem to work for all libraries. E.g. (found here):

pip install Pillow

is required to get this to succeed

import PIL

Based on the pattern in the first examples, I would have expected pip install PIL to install PIL, but instead we use pip install Pillow. Why is this and how does this work?

stevec
  • 41,291
  • 27
  • 223
  • 311
  • 1
    from Pillow docs: ```Pillow is the friendly PIL fork by Alex Clark and Contributors. PIL is the Python Imaging Library by Fredrik Lundh and Contributors.```. Package itself is called Pillow, but it is actually PIL fork. Thats why you import PIL. – token Jul 10 '20 at 12:27
  • 1
    https://stackoverflow.com/a/54599368/7976758 – phd Jul 10 '20 at 12:45

1 Answers1

1

Basically, what you import is usually the module name. For example, your package might be developed in the following hierarchy:

MyLib
- __init__.py
- my_script1.py
- my_script2.py

However, when you make your library as a "package" available in pip, usually you will need to prepare your setup.py file, which will be automatically run when people use pip install to install your package.

The setup.py can be something like this:

from distutils.core import setup
setup(
  name = 'YOURPACKAGENAME',         # How you named your package folder (MyLib)
  packages = ['YOURPACKAGENAME'],   # Chose the same as "name"
  version = '0.1',      # Start with a small number and increase it with every change you make
  license='MIT',        # Chose a license from here: https://help.github.com/articles/licensing-a-repository
  description = 'TYPE YOUR DESCRIPTION HERE',   # Give a short description about your library
  author = 'YOUR NAME',                   # Type in your name
  author_email = 'your.email@domain.com',      # Type in your E-Mail
  url = 'https://github.com/user/reponame',   # Provide either the link to your github or to your website
  download_url = 'https://github.com/user/reponame/archive/v_01.tar.gz',    # I explain this later on
  keywords = ['SOME', 'MEANINGFULL', 'KEYWORDS'],   # Keywords that define your package best
  install_requires=[            # I get to this in a second
          'validators',
          'beautifulsoup4',
      ],
  classifiers=[
    'Development Status :: 3 - Alpha',      # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package
    'Intended Audience :: Developers',      # Define that your audience are developers
    'Topic :: Software Development :: Build Tools',
    'License :: OSI Approved :: MIT License',   # Again, pick a license
    'Programming Language :: Python :: 3',      #Specify which pyhton versions that you want to support
    'Programming Language :: Python :: 3.4',
    'Programming Language :: Python :: 3.5',
    'Programming Language :: Python :: 3.6',
  ],
)

Therefore, in the above example, people who install your package via pip should run pip install YOURPACKAGENAME. After that, they need to run import MyLib in the code.

TD; DL:

What you import is a module name, but what you installed via pip is the name of the package, they can be different. But usually, I would say that I like people to use the same name for both to avoid any confusing.

Ref: https://medium.com/@joel.barmettler/how-to-upload-your-python-package-to-pypi-65edc5fe9c56

Christopher
  • 731
  • 6
  • 24
  • Thanks, great explanation! Is there a way to find the package name (to use with pip)? E.g. I need to run `import inception5h`, but `ERROR: No matching distribution found for inception5h`. With `PIL` I was able to find the package name by googling, and thankfully others had had the same question. But with `inception5h` I cannot find the package name easily. Is there a way to do that? – stevec Jul 10 '20 at 12:49
  • 1
    Maybe there is, but I don't know honestly. I met the same issue before, I would say just google it :) Try googling `install sklearn` you will know what I mean. – Christopher Jul 10 '20 at 12:53