Giuliani Sanches
Giuliani Sanches

Reputation: 672

Problems creating a python package using setuptools and trying to install it from local filesystem

I have created a small package and I'm trying to install it from the local file system but things are not working as expected.

So, let's start with my repository structure

python
    packages
        myapiclient
            myapi
                __init__.py
                apiclient.py
            requirements.txt
            setup.py

The init file is empty.

apiclient.py

class APIClient(object):
  pass

setup.py

# -*- coding: utf-8 -*-

from setuptools import setup, find_packages

setup(
    name='myapi',
    version='0.1.0',
    description='MyApi API client',
    author='Giuliani D. Sanches',
    author_email='[email protected]',
    packages=find_packages(exclude=('tests', 'docs')),
    install_requires=['requests']
)

To install my package using a virtualenv I run the following pip install command but it finish with an error:

(.vevn) $ pip install --upgrade --no-index --find-links /lib-repository/python/packages/myapiclient/ myapi
Looking in links: /lib-repository/python/packages/myapiclient/
ERROR: Could not find a version that satisfies the requirement myapi (from versions: none)
ERROR: No matching distribution found for myapi

Doing a setup.py install seems to work:

(.venv) $ python3 /lib-repository/python/packages/myapiclient/setup.py install --force

output

running install
running bdist_egg
running egg_info
writing myapi.egg-info/PKG-INFO
writing dependency_links to myapi.egg-info/dependency_links.txt
writing requirements to myapi.egg-info/requires.txt
writing top-level names to myapi.egg-info/top_level.txt
reading manifest file 'myapi.egg-info/SOURCES.txt'
writing manifest file 'myapi.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
warning: install_lib: 'build/lib' does not exist -- no Python modules to install

creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying myapi.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying myapi.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying myapi.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying myapi.egg-info/requires.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying myapi.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist/myapi-0.1.0-py3.8.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing myapi-0.1.0-py3.8.egg
Copying myapi-0.1.0-py3.8.egg to /lib-repository/.venv/lib/python3.8/site-packages
Adding myapi 0.1.0 to easy-install.pth file

Installed /lib-repository/.venv/lib/python3.8/site-packages/myapi-0.1.0-py3.8.egg
Processing dependencies for myapi==0.1.0
Searching for requests==2.27.1
Best match: requests 2.27.1
Adding requests 2.27.1 to easy-install.pth file

Using /lib-repository/.venv/lib/python3.8/site-packages
Searching for idna==3.3
Best match: idna 3.3
Adding idna 3.3 to easy-install.pth file

Using /lib-repository/.venv/lib/python3.8/site-packages
Searching for charset-normalizer==2.0.10
Best match: charset-normalizer 2.0.10
Adding charset-normalizer 2.0.10 to easy-install.pth file
Installing normalizer script to /lib-repository/.venv/bin

Using /lib-repository/.venv/lib/python3.8/site-packages
Searching for certifi==2021.10.8
Best match: certifi 2021.10.8
Adding certifi 2021.10.8 to easy-install.pth file

Using /lib-repository/.venv/lib/python3.8/site-packages
Searching for urllib3==1.26.8
Best match: urllib3 1.26.8
Adding urllib3 1.26.8 to easy-install.pth file

Using /lib-repository/.venv/lib/python3.8/site-packages
Finished processing dependencies for myapi==0.1.0
(.vevn) $ pip list
Package            Version
------------------ ---------
build              0.7.0
certifi            2021.10.8
charset-normalizer 2.0.10
idna               3.3
packaging          21.3
pep517             0.12.0
pip                20.0.2
pkg-resources      0.0.0
pyparsing          3.0.6
myapi              0.1.0
requests           2.27.1
setuptools         44.0.0
tomli              2.0.0
urllib3            1.26.8

But when I try to import it, I get a ModuleNotFoundError exception:

(.venv) $ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import myapi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'myapi'
>>>

The pip install command works just fine after the setup.py install

(.venv) $ pip install --upgrade --no-index --find-links /lib-repository/python/packages/myapiclient/ myapi
Looking in links: /lib-repository/python/packages/myapiclient/
Requirement already up-to-date: myapi in ./.venv/lib/python3.8/site-packages/myapi-0.1.0-py3.8.egg (0.1.0)
Requirement already satisfied, skipping upgrade: requests in ./.venv/lib/python3.8/site-packages (from myapi) (2.27.1)
Requirement already satisfied, skipping upgrade: idna<4,>=2.5; python_version >= "3" in ./.venv/lib/python3.8/site-packages (from requests->myapi) (3.3)
Requirement already satisfied, skipping upgrade: charset-normalizer~=2.0.0; python_version >= "3" in ./.venv/lib/python3.8/site-packages (from requests->myapi) (2.0.10)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in ./.venv/lib/python3.8/site-packages (from requests->myapi) (2021.10.8)
Requirement already satisfied, skipping upgrade: urllib3<1.27,>=1.21.1 in ./.venv/lib/python3.8/site-packages (from requests->myapi) (1.26.8)

(.venv) $

What am I missing here ?

Upvotes: 1

Views: 1130

Answers (1)

Giuliani Sanches
Giuliani Sanches

Reputation: 672

Well.. after some trial and error, i found a solution:

(.venv) $ pip3 install /lib-repository/python/packages/myapiclient/

You just need to run the install command pointing to the directory containing the setup.py file.

No nee to use --no-index or --find-links in this case.

Now I have everything in place! :)

Thank you very much!

Upvotes: 1

Related Questions