Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create a Python package for pip

I have been unsuccessful in creating a Python package distribution. I have followed the instructions in https://packaging.python.org/tutorials/packaging-projects/ and this is what happens. For e.g. if I create the necessary files and folders as required

first/
   first/
     __init__.py
     setup.py
     LICENSE
     README.md
     myfirst.py

I have made the package name as 'first' in setup.py

import setuptools

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

    setuptools.setup(
        name="tvg11",
        version="0.0.1",
        author="Ganesh",
        author_email="[email protected]",
        description="A small example package",
        long_description=long_description,
        long_description_content_type="text/markdown",
        url="https://github.com/pypa/sampleproject",
        packages=setuptools.find_packages(),
        classifiers=[
            "Programming Language :: Python :: 3",
            "License :: OSI Approved :: MIT License",
            "Operating System :: OS Independent",
        ],
    )

Then I run the commands

python setup.py sdist bdist_wheel
python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*

which creates dist/, build/ and first.egg-info.

Now I find that if I am in the same directory and do a

pip install --index-url https://test.pypi.org/simple/ first

Then I find that this creates a new folder pycache with the compiled code and I am able to do import first

first/
  first/
    __pycache__     # new!
    build/
    dist/
    first.egg.info
    __init__.py
    setup.py
    LICENSE
    README.md
    myfirst.py

However, if I am in any other folder other the root folder 'first/first' and I do a

pip install --index-url https://test.pypi.org/simple/ first

I get a 'Successfully installed' but when I invoke python and do an import first I get ModuleNotFoundError "No module 'first'".

Please let me know what I am doing wrong. The instructions were so simple.

like image 387
Tinniam V. Ganesh Avatar asked Aug 27 '26 00:08

Tinniam V. Ganesh


1 Answers

You should have looked interactively in test.pypi.org ...

You would have immediately noticed that your own project in not known as first but as tvg11. What matters is the name and version number inside the setup.py file and not the folder name.

But the real cause is that you have put the files of the package at same level as the setup.py file and used packages=setuptools.find_packages(). So as you have no packages below setup.py, find_packages found nothing, and you have built and interesting package containing little more than setup.py and README.md.

How to fix:

First you should build a more common structure for your package:

  - first
    - setup.py
    - LICENSE
    - README.md

    - first
        - __init__.py
        - myfirst.py

That way find_package will find the content of the first subfolder as the source for the tvg11 package.

like image 182
Serge Ballesta Avatar answered Aug 29 '26 13:08

Serge Ballesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!