Skip to content
Advertisement

PyPI personal module cannot be imported by other users or myself?

I am currently working on a rather simply Python module and I’ve been trying to publish it on PyPI all day. This is my first time doing it.

I’ve succesfully built it using python3 setup.py sdist bdist_wheel then done python3 -m twine upload --repository testpypi dist/* and twine upload dist/* and it is successfully uploaded as seen here. However, when me or my friends do pip install manhattandistance it installs the package, and when I try to import it using from manhattan distance import * as seen in my code:

from manhattandistance import *
print(mandist(37.99438337141694, 23.732534940640544, 37.97163377228043, 23.72572774064004))

However, it says that Import "manhattan_distance" could not be resolved. When I try reinstalling it using pip, it says that it already is installed, and when I make my way to that path I notice that only a .info is installed, while other modules have 2 folders, one with files inside and one for .info.

Screenshot from powershell: Screenshot from powershell

See that my module only has a .info folder dedicated?: See that my module only has a .info folder dedicated

I created all the necessary files:

-manhattandistance
--src
---__init.py
--LICENSE.txt
--CHANGELOG.txt
--READDME.md
--setup.py

My setup.py

from setuptools import setup, find_packages
 
with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()
 
setup(
  name='manhattandistance',
  py_modules=["manhattandistance"],
  version='1.0.3',
  description='Calculates the Manhattan Distance of two places',
  long_description=open('README.md').read() +  open('CHANGELOG.txt').read(),
  long_description_content_type="text/markdown",
  url='http://packages.python.org/manhattandistance',  
  author='NAME',
  author_email='MAIL',
  license='MIT License', 
  classifiers=[
  'Development Status :: 5 - Production/Stable',
  'Intended Audience :: Education',
  'Operating System :: Microsoft :: Windows :: Windows 10',
  'License :: OSI Approved :: MIT License',
  'Programming Language :: Python :: 3'
                ],
  keywords='distance', 
  package_dir={'':'src'},
)

My init.py

def mandist(lat_from, lon_from, lat_to, lon_to):
    from math import cos
    phi_m = 3.141592653589793/180 * (lat_from + lat_to) / 2
    lat_k = 111.13209 - 0.56605 * cos(2 * phi_m) + 0.00120 * cos(4 * phi_m)
    lon_k = 111.41513 * cos(phi_m) - 0.0945 * cos(3 * phi_m) + 0.00012*cos(5 * phi_m)
    fin_lat = (lat_from - lat_to) * lat_k
    fin_lon = (lon_from - lon_to) * lon_k
    return abs(fin_lon) + abs(fin_lat)

Advertisement

Answer

Try with: import manhattandistance

And then use as follow: manhattandistance.mandist()

ps: I suggest to have another file instead of putting it in the __init__.py

EDIT:

Place your code in a utils.py file. You will have a structure like this:

manhattandistance/
    src/
        __init__.py
        utils.py
    LICENSE.txt
    CHANGELOG.txt
    READDME.md
    setup.py

Change your settings by replacing package_dir to packages=setuptools.find_packages() (should be enough)

    setup(
      name='manhattandistance',
      version='1.0.3',
      description='Calculates the Manhattan Distance of two places',
      long_description=open('README.md').read() +  open('CHANGELOG.txt').read(),
      long_description_content_type="text/markdown",
      url='http://packages.python.org/manhattandistance',  
      author='Dionysios Rigatos',
      author_email='dion.rigatos@gmail.com',
      license='MIT License', 
      classifiers=[
      'Development Status :: 5 - Production/Stable',
      'Intended Audience :: Education',
      'Operating System :: Microsoft :: Windows :: Windows 10',
      'License :: OSI Approved :: MIT License',
      'Programming Language :: Python :: 3'
                    ],
      keywords='distance', 
      packages=setuptools.find_packages(),
    )

then you should be able to import the module with:

from manhattandistance.src.utils import mandist

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement