Search code examples
pythonlinuxpippython-importpython-wheel

Why customer python package can not be imported?


I created my own python package named jjnsegutils and upload it to Pypi website. But after I successfully install it by pip install jjnsegutils, I still can not import it. Error shows: ModuleNotFoundError: No module named 'jjnsegutils'.


Details about the whole procedure are as follows.

Package structure and details

The structure of my package is:

jjnsequtils
├─ __init__.py
├─ myutil
    ├─ __init__.py
    ├─ myutil.py
├─ LICENSE
├─ README.md
├─ setup.py

Two __init__.py files are empty.

In my setup.py:

import setuptools

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

setuptools.setup(
    name="jjnsegutils", # Replace with your own username
    version="0.0.10",
    author="Jingnan",
    author_email="[email protected]",
    description="A package of common utilities for Medical images segmentation and evaluation.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/Ordgod/jjnsegutils",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)

How do I generate the package?

I use command python setup.py sdist bdist_wheel to generate distribute files. logging information:

running sdist
running egg_info
writing jjnsegutils.egg-info/PKG-INFO
writing dependency_links to jjnsegutils.egg-info/dependency_links.txt
writing top-level names to jjnsegutils.egg-info/top_level.txt
reading manifest file 'jjnsegutils.egg-info/SOURCES.txt'
writing manifest file 'jjnsegutils.egg-info/SOURCES.txt'
running check
creating jjnsegutils-0.0.10
creating jjnsegutils-0.0.10/jjnsegutils.egg-info
creating jjnsegutils-0.0.10/myutil
copying files to jjnsegutils-0.0.10...
copying README.md -> jjnsegutils-0.0.10
copying setup.py -> jjnsegutils-0.0.10
copying jjnsegutils.egg-info/PKG-INFO -> jjnsegutils-0.0.10/jjnsegutils.egg-info
copying jjnsegutils.egg-info/SOURCES.txt -> jjnsegutils-0.0.10/jjnsegutils.egg-info
copying jjnsegutils.egg-info/dependency_links.txt -> jjnsegutils-0.0.10/jjnsegutils.egg-info
copying jjnsegutils.egg-info/top_level.txt -> jjnsegutils-0.0.10/jjnsegutils.egg-info
copying myutil/__init__.py -> jjnsegutils-0.0.10/myutil
copying myutil/myutil.py -> jjnsegutils-0.0.10/myutil
Writing jjnsegutils-0.0.10/setup.cfg
Creating tar archive
removing 'jjnsegutils-0.0.10' (and everything under it)
running bdist_wheel
running build
running build_py
installing to build/bdist.linux-x86_64/wheel
running install
running install_lib
creating build/bdist.linux-x86_64/wheel
copying build/lib/myutil.py -> build/bdist.linux-x86_64/wheel
creating build/bdist.linux-x86_64/wheel/myutil
copying build/lib/myutil/__init__.py -> build/bdist.linux-x86_64/wheel/myutil
copying build/lib/myutil/myutil.py -> build/bdist.linux-x86_64/wheel/myutil
running install_egg_info
Copying jjnsegutils.egg-info to build/bdist.linux-x86_64/wheel/jjnsegutils-0.0.10-py3.7.egg-info
running install_scripts
adding license file "LICENSE" (matched pattern "LICEN[CS]E*")
creating build/bdist.linux-x86_64/wheel/jjnsegutils-0.0.10.dist-info/WHEEL
creating 'dist/jjnsegutils-0.0.10-py3-none-any.whl' and adding 'build/bdist.linux-x86_64/wheel' to it
adding 'myutil.py'
adding 'myutil/__init__.py'
adding 'myutil/myutil.py'
adding 'jjnsegutils-0.0.10.dist-info/LICENSE'
adding 'jjnsegutils-0.0.10.dist-info/METADATA'
adding 'jjnsegutils-0.0.10.dist-info/WHEEL'
adding 'jjnsegutils-0.0.10.dist-info/top_level.txt'
adding 'jjnsegutils-0.0.10.dist-info/RECORD'
removing build/bdist.linux-x86_64/wheel

How do I distribute the package?

I use command twine upload dist/* to upload the files in dist to Pypi.

How do I install the package?

I use command pip install jjnsegutils to install it into my own environment named py37 created by conda. It shows

Collecting jjnsegutils
  Downloading jjnsegutils-0.0.10-py3-none-any.whl (11 kB)
Installing collected packages: jjnsegutils
Successfully installed jjnsegutils-0.0.10

Then I type $ python in my terminal to enter the python interractive terminal. and type:

>>> import jjnsegutils

But it shows:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'jjnsegutils'

What actions I have tried to diagnosize the issue?

I checked the package by command $ pip show jjnsegutils, it shows:

Name: jjnsegutils
Version: 0.0.10
Summary: A package of common utilities for Medical images segmentation and evaluation.
Home-page: https://github.com/Ordgod/jjnsegutils
Author: Jingnan
Author-email: [email protected]
License: UNKNOWN
Location: /home/jjia/.conda/envs/py37/lib/python3.7/site-packages
Requires:
Required-by:

And I checked it futher by $ ls /home/jjia/.conda/envs/py37/lib/python3.7/site-packages, it shows:

... # other packages
jjnsegutils-0.0.10.dist-info                
... # other packages

I thought there should be anotehr directory named jjnsegutils along with jjnsegutils-0.0.10.dist-info. But I did not see it. Is this the reason that I can not import my own package?

I make sure that the virtual environment is always the same one named py37 during the whole procedure. I used CentOS, python3.7.

Looking forward to any discussion and advice. Thanks very much!


Solution

  • You should import myutil.

    packages is a list of all Python import packages that should be included in the Distribution Package. Instead of listing each package manually, we can use find_packages() to automatically discover all packages and subpackages. In this case, the list of packages will be example_pkg as that’s the only package present. https://packaging.python.org/tutorials/packaging-projects/