简体   繁体   中英

Python cannot find newly installed module

I've created a module using the following setup.py

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

# Learn more: https://github.com/kennethreitz/setup.py

from setuptools import setup, find_packages


with open('README.md') as f:
    readme = f.read()

with open('LICENSE') as f:
    license = f.read()

setup(
    name='mymod',
    version='1.0a1',
    description='test',
    long_description=readme,
    long_description_content_type="text/markdown",
    author='Ray Salemi',
    author_email='ray@raysalemi.com',
    url='https://rayboston@bitbucket.org/rayboston/mymod',
    license=license,
    packages=find_packages(exclude=('tests', 'docs', 'examples'))
)

But when I try to install it using

% python setup.py install

I see that it gets installed in my site packages:

Processing mymod-1.0a1-py3.8.egg
Copying mymod-1.0a1-py3.8.egg to /Users/raysalemi/PycharmProjects/testenv/lib/python3.8/site-packages
Adding mymod 1.0a1 to easy-install.pth file

Installed /Users/raysalemi/PycharmProjects/testenv/lib/python3.8/site-packages/mymod-1.0a1-py3.8.egg
Processing dependencies for mymod==1.0a1
Finished processing dependencies for mymod==1.0a1
(testenv) (base) raysalemi@WriteNow mymod % cd ../testenv
(testenv) (base) raysalemi@WriteNow testenv % python
Python 3.8.3 (default, Jul  2 2020, 11:26:31)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymod
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mymod'

How do I debug this? I can't see an error.

I'm running Big Sur 11.0.1 and Python 3.8.3 from Anaconda

Pip shows the module is there

Package    Version
---------- -------
pip        20.3.1
mymod      1.0a1
setuptools 41.2.0

The problem is that the package is being misnamed:

(testenv) (base) raysalemi@WriteNow site-packages % ls
__pycache__         mymod-1.0a0-py3.8.egg
easy-install.pth        mymod-1.0a0.dist-info
easy_install.py         setuptools
pip             setuptools-41.2.0.dist-info
pip-20.3.1.dist-info        src
pkg_resources

It is mymod-1.0a0-py3.8.egg instead of mymod

To debug you can run the setup:

python setup.py sdist --formats=gztar

and unzip the resulting .tar.gz file and check if all your source code files are in it. (or use --formats=zip instead of gztar to get a simpler file to extract)

The resulting package is always of the form package_name-package_version , so the name you received is not incorrect. (In case you are wondering, you can find the valid package_version formatting rules here .)

You can later use this package by adding it to the requirements.txt file of the project you want to be dependent on it. Eg

my-package>=1.2.0,<2.0.0

In your case, since the version is a pre-release ( mymod-1.0a0-py3.8.egg ==> version is 1.0a0-py3.8.egg which means version 1.0 pre-relase version alpha0-py3.8 ).

The version 1.0a0-py3.8.egg < than version 1.0 (pre-release always < release with same number), so you will need something like >0,<2.0 .

Personally, I put the source code in the repo under src/ and then select these files in setup.py using:

packages=find_namespace_packages(where="src")

There are other parameters I recommend using eg make sure environment has a new enough setuptools to recognize find_namespace_packages , take list of dependencies from requirements.txt files etc.:

from setuptools import setup, find_namespace_packages


with open('requirements.txt') as f:
    required = f.read().splitlines()

setup(
    name='your_project_name',
    version='1.0.0',
    description='your project description',
    url='your repo url',
    author='your username',
    author_email='your email',
    license='your license type',
    package_dir={'': 'src'},
    setup_requires='setuptools>=45.2.0',
    packages=find_namespace_packages(where="src"),
    install_requires=required,
    data_files=['requirements.txt'],
    include_package_data=True
)

See the full list of options and what they are for in the documentation .

I found my problem.

My source directory was named src not mymod . So there was a src directory in site-packages instead of a mymod directory. This is a surprise since the package is named in setup.py .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM