Piash
Piash

Reputation: 1131

installing from setup.py but ModuleNotFoundError

I was trying to make a python package. This is my package structure.

my-package  
├── README.md  
├── LICENSE  
├── .gitignore  
├── setup.py  
└── my-package  
    ├── __init__.py
    └── other_files.py

in my setup.py:

import io
import os
from setuptools import setup, find_packages, find_namespace_packages

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

setup(
    name="new-package",    # This is the name of the package
    version="1.0.0",      # The initial release version
    author="Author Name", # Full name of the author
    author_email='[email protected]',
    url='https://github.com/Author/myPackage',
    description="Short Descriptio",
    long_description=long_description,      # Long description read from the the readme file
    long_description_content_type="text/markdown",
    packages=find_namespace_packages(),    # List of all python modules to be installed
    classifiers=[
        'Intended Audience :: Developers',
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        'Topic :: Software Development :: Internationalization',
    ],                                      # Information to filter the project on PyPi website
    python_requires='>=3.5',                # Minimum version requirement of the package
    install_requires=['requests','simplejson'],  # Install other dependencies if any
)

from the root directory, I am generating distribution files using this command:

python setup.py sdist bdist_wheel

Then, I am installing this package in a virtualenvironment using this command, from the package root directory:

python -m pip install -e .

After installing, if I run pip list, it shows me that this package (new_package) is installed.

but when I try to import this, using import new_package, it gives me

ModuleNotFoundError: No module named 'new_package'

I have searched in the stack for similar problem, but could not solve it.

Any help ?

Upvotes: 0

Views: 1558

Answers (1)

cizario
cizario

Reputation: 4254

try to rename the inner my-package folder to my_package as hyphen is not a valide name for packages or modules to import

my-package  # root folder
├── README.md  
├── LICENSE  
├── .gitignore  
├── setup.py  
└── my_package  # HERE, this package   
    ├── __init__.py
    └── other_files.py

and rebuild and reinstall the distribution

Note: the name of root folder my-package doesn't matter if it contains a hyphen because it's meant for publishing on pypi repository and for pip command to install the package

Upvotes: 1

Related Questions