kramer65
kramer65

Reputation: 53853

Why can't I import my self-created Python package?

I just created a Python package (sources here) but I'm having trouble using it after installation. A simple import gives me an ImportError.

Below I'll show exactly what I did so that you can reproduce it:

$ git clone [email protected]:kramer65/peewee-versioned.git
Cloning into 'peewee-versioned' 
etc. etc.
Checking connectivity... done.
$ virtualenv venv
New python executable in the/path/to/my/venv/bin/python
Installing setuptools, pip, wheel...done.
$ . venv/bin/activate
(venv) $ cd peewee-versioned/
(venv) $ python setup.py install
running install
etc. etc. everything installs without errors
Finished processing dependencies for peewee-versioned==0.1
(venv) $ cd ..
(venv) $ pip freeze
peewee==2.8.0
peewee-versioned==0.1  # AS YOU CAN SEE IT IS INSTALLED
six==1.10.0
(venv) $ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import peewee
>>> import peewee_versioned
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named peewee_versioned

My setup.py looks like this:

from setuptools import setup, find_packages

setup(
    name='peewee-versioned',
    version='0.1',
    packages=find_packages(exclude=['test', 'test.*']),
    include_package_data=True,
    platforms='any',
    install_requires=[
        'peewee',
        'six'
    ],
)

and the only relevant file in the project is versioned.py (see the full sources here)

Does anybody have any idea what I'm doing wrong here? All tips are welcome!

[EDIT]

I checked whether there was any egg related to my package in venv site-packages, and there is (check the 5th package):

$ ls -l venv/lib/python2.7/site-packages/
total 512
-rw-r--r--   1 kramer65  staff     276 13 apr 13:53 easy-install.pth
-rw-r--r--   1 kramer65  staff     126 13 apr 13:53 easy_install.py
-rw-r--r--   1 kramer65  staff     367 13 apr 13:53 easy_install.pyc
-rw-r--r--   1 kramer65  staff  242923 13 apr 13:53 peewee-2.8.0-py2.7.egg
-rw-r--r--   1 kramer65  staff     970 13 apr 13:53 peewee_versioned-0.1-py2.7.egg
drwxr-xr-x  34 kramer65  staff    1156 13 apr 13:53 pip
drwxr-xr-x  10 kramer65  staff     340 13 apr 13:53 pip-8.1.1.dist-info
drwxr-xr-x   6 kramer65  staff     204 13 apr 13:53 pkg_resources
drwxr-xr-x  52 kramer65  staff    1768 13 apr 13:53 setuptools
drwxr-xr-x  12 kramer65  staff     408 13 apr 13:53 setuptools-20.7.0.dist-info
drwxr-xr-x   5 kramer65  staff     170 13 apr 13:53 six-1.10.0-py2.7.egg
drwxr-xr-x  32 kramer65  staff    1088 13 apr 13:53 wheel
drwxr-xr-x  11 kramer65  staff     374 13 apr 13:53 wheel-0.29.0.dist-info

So why oh why can't I import it?

Upvotes: 2

Views: 390

Answers (4)

rohithpr
rohithpr

Reputation: 6330

The correct way to do this is to have your directory structured like this:

peewee-versioned/
  peewee_versioned/
    __init__.py
    peewee_versioned.py
  setup.py

where __init__.py has the line import peewee_versioned (or from peewee_versioned import *) depending on your use cases.

By structuring your files properly you can protect yourself against future changes where you may have more than one file.

Upvotes: 0

joaonrb
joaonrb

Reputation: 1021

I think you have to include the versioned module individually since you have no package. I don't think the find_package method will find solo modules.

Try adding py_modules = ['versioned'] to setup like:

setup(
    name='peewee-versioned',
    version='0.1.1',
    packages=find_packages(exclude=['test', 'test.*']),
    include_package_data=True,
    platforms='any',
    install_requires=[
        'peewee',
        'six'
    ],
    py_modules = ['versioned']
)

After this, you should be able to import versioned.

Upvotes: 2

David Arcos
David Arcos

Reputation: 6047

If you just want the name, you could use the "import as" feature:

import versioned as peewee_versioned

Upvotes: 1

alecxe
alecxe

Reputation: 473823

Since you don't have any packages in the repository and only have a single file named versioned.py, you should be able to import it as import versioned after running python setup.py install. Works for me:

$ mkvirtualenv peewee
New python executable in peewee/bin/python
...
$ git clone https://github.com/kramer65/peewee-versioned
Cloning into 'peewee-versioned'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 20 (delta 5), reused 18 (delta 5), pack-reused 0
Unpacking objects: 100% (20/20), done.
Checking connectivity... done.
$ cd peewee-versioned/
$ python setup.py install
running install
...
Finished processing dependencies for peewee-versioned==0.1
$ pip freeze
peewee==2.8.0
peewee-versioned==0.1
six==1.10.0
wheel==0.24.0
$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import versioned
>>>

Anyway, since you want it to be imported from peewee_versioned, I would do two things here:

  • rename versioned.py to peewee_versioned.py
  • follow the Listing individual modules documentation paragraph, use py_modules:

    py_modules = ['peewee_versioned']
    

Upvotes: 5

Related Questions