Installed module is empty

此生再无相见时 提交于 2019-12-10 21:21:42

问题


I'm tring to use setuptools for python3 code. My project structure:

./testSetup/
./testSetup/testSetup
./testSetup/testSetup/foo.py
./testSetup/Setup.py

./testSetup/testSetup/foo.py content:

def say_foo():
    print('foo')

./testSetup/Setup.py content:

from setuptools import setup, find_packages
import testSetup

setup(
    name='testSetup',
    version='0.0.1',
    packages=find_packages(),
    author='Bastien Sevajol',
    author_email="testSetup@bux.fr",
    description='test',
    long_description='test test',
    include_package_data=False,
    url='http://bux.fr',
    # https://pypi.python.org/pypi?%3Aaction=list_classifiers.
    classifiers=[
        "Programming Language :: Python",
    ]
)

When i python Setup.py install (with python3.4) in a virtual env:

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

creating build
creating build/bdist.linux-i686
creating build/bdist.linux-i686/egg
creating build/bdist.linux-i686/egg/EGG-INFO
copying testSetup.egg-info/PKG-INFO -> build/bdist.linux-i686/egg/EGG-INFO
copying testSetup.egg-info/SOURCES.txt -> build/bdist.linux-i686/egg/EGG-INFO
copying testSetup.egg-info/dependency_links.txt -> build/bdist.linux-i686/egg/EGG-INFO
copying testSetup.egg-info/top_level.txt -> build/bdist.linux-i686/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/testSetup-0.0.1-py3.4.egg' and adding 'build/bdist.linux-i686/egg' to it
removing 'build/bdist.linux-i686/egg' (and everything under it)
Processing testSetup-0.0.1-py3.4.egg
Copying testSetup-0.0.1-py3.4.egg to /home/bux/.virtualenvs/testsynergine2/lib/python3.4/site-packages
Adding testSetup 0.0.1 to easy-install.pth file

Installed /home/bux/.virtualenvs/testsynergine2/lib/python3.4/site-packages/testSetup-0.0.1-py3.4.egg
Processing dependencies for testSetup==0.0.1
Finished processing dependencies for testSetup==0.0.1

I can see a warning saying install_lib: 'build/lib' does not exist -- no Python modules to install.

If i try to use my module:

python               
Python 3.4.0 (default, Apr 11 2014, 13:05:18) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from testSetup import foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'foo'
>>> import testSetup
>>> dir(testSetup)
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

What's wrong in my Setup.py ?


回答1:


You need to have an __init__.py file in your TestSetup/ directory (the file can be blank). Otherwise, nothing in that directory will be importable and find_packages() will never find it. You might want to read about it.

./testSetup/
./testSetup/testSetup
./testSetup/testSetup/__init__.py
./testSetup/testSetup/foo.py
./testSetup/setup.py

Also note that setup.py should be all lowercase. While naming it Septup.py won't stop the script from working, it is not convention and may not be discovered by various tools.




回答2:


It is because of setuptools.find_packages, when calling it without any parameters it looks for packages in the source tree from the location of setup.py. In your case you don't have any folder at the root level.

The issue does not come from your setup.py file but from the way you've organized your project, by moving your sources around like so:

testSetup
|--setup.py
|--/testSetup/foo.py
|--/testSetup/__init__.py

then you can successfully pip setup.py install when cd-ing inside testSetup.

some doc on find_packages



来源:https://stackoverflow.com/questions/30353218/installed-module-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!