2

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 git@github.com: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?

kramer65
  • 50,427
  • 120
  • 308
  • 488
  • You may want to show your `setup.py` and the structure of your python project as well. – cel Apr 13 '16 at 11:28
  • @cel - You're right. I just added setup.py and a link at the end to the sources. Any idea? – kramer65 Apr 13 '16 at 11:33
  • `import peewee_versioned` cannot work, because you have not installed a module with that name. Your module's name is `versioned`, so `import versioned` probably works. – cel Apr 13 '16 at 11:34
  • `import peewee-versioned` maybe? – Eugene Soldatov Apr 13 '16 at 11:35
  • @cel - Unfortunately `import versioned` also gives an `ImportError`. I tried renaming `versioned.py` to `peewee_versioned.py` and installing it again, but that gives the same result. Any other ideas? – kramer65 Apr 13 '16 at 11:51

4 Answers4

5

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']
    
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2

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.

joaonrb
  • 965
  • 11
  • 30
1

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

import versioned as peewee_versioned
David Arcos
  • 5,957
  • 5
  • 30
  • 39
0

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.

rohithpr
  • 6,050
  • 8
  • 36
  • 60