简体   繁体   中英

ModuleNotFoundError: No module named my package but I can find it in pip freeze

I tried to make a custom package [(link)][1] like this

[ '|-- willytricks',
  '    |-- __init__.py',
  '    |-- directoryList.md',
  '    |-- setup.py',
  '    |-- trick.py',
  '    |-- build',
  '    |   |-- bdist.linux-x86_64',
  '    |-- dist',
  '    |   |-- willytricks-0.0.1-py3.6.egg',
  '    |   |-- willytricks-0.0.1.tar.gz',
  '    |-- willytricks.egg-info',
  '        |-- PKG-INFO',
  '        |-- SOURCES.txt',
  '        |-- dependency_links.txt',
  '        |-- top_level.txt',
  '
]

the setup.py :

import setuptools

setuptools.setup(
    name="willytricks",
    version="0.0.1",
    author="wil75822",
    author_email="lunasdejavu@gmail.com",
    description="",
    long_description=" useful python tools",
    long_description_content_type="text",
    url="www.ntu.edu.tw",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)

then I type python setup.py sdist

sudo pip3 install "~/dist/willytricks-0.0.1.tar.gz"

I can find it by pip3 freeze the information:

willytricks @ file:///data/workspace/willy_sung/willytricks/dist/willytricks-0.0.1.tar.gz

but I can't import willytricks in python3

import willytricks
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'willytricks'

thank you guys! after following your tips I can import willytricks now but it shows another error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/workspace/willy_sung/myproject/dist/willytricks-0.0.1/willytricks/__init__.py", line 1, in <module>
    from trick import *
ModuleNotFoundError: No module named 'trick'

what should I put inside of init .py? [1]: https://l.messenger.com/l.php?u=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F1rrS_JzOMcwx7b_-6xkF85Jo8GQnp3H0M%2Fview%3Fusp%3Dsharing&h=AT1V7YbJgVgstXoSU7qiD6wdO-rjPdcXjNJ6HVTWfZ0y-zNFF4nSiLBvXZRDU8OC05PiZZGg8V9VyecHgxUJdlrKR152cyh5VUALDtdFE4KZ3RQsfe8yR9rznQLflTbwr2o

Python distribution packages don't have to be named the same as the scripts, modules and packages they actually deliver. When you ran setup.py in willytricks it didn't find any packages, and it built a "willytricks" distribution without any packages in it.

If I run the scanner manually,

>>> import os
>>> os.getcwd()
'/home/td/tmp/bbb/willytricks'
>>> import setuptools
>>> setuptools.find_packages()
[]

But if I go back a directory and scan again, a package is found

>>> os.chdir("..")
>>> os.getcwd()
'/home/td/tmp/packagedir'
>>> setuptools.find_packages()
['willytricks']

You need to move setup.py , and everything that is not part of the package up one directory. This directory can be named anything. In fact its common to name it the same as the package. Its common to use source control and you could have multiple copies of these files checked out in different directories at the same time.

Change your structure to the following and it should work.

<projectdir>--|-- willytricks
  |           |-- __init__.py
  |           |-- trick.py
  |-- directoryList.md
  |-- setup.py
  |-- build
        ...
  |-- dist
        ...
  |-- willytricks.egg-info
        ...

Scripts and Modules and Packages

A python "package" is a directory with .py modules and subpackages. Its a bit unfortunate that we end up talking about packaging packages. Modules and packages are installed into background directories known by python (perhaps site-packages or dist-packages somewhere in the python distribution). Top level scripts are also .py files but are usually located where the operating system can find them in the system PATH .

Since you have a package with only one module, you could easily distribute it as a single module instead. You only need a package if you have multiple python modules that you want to group together - or plan to do so in the future. For a single module distribution, you can go back to your original structure and remove __init__.py . In setup.py remove packages=setuptools.find_packages(), and add py_modules=["Tricks"], . Now, import tricks would work. You could also rename that .py to willytricks.py for import willytricks to work.

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