3

Short summary

I can upload a package to pyPI, and install it with pip, but Python is not seeing it when I try to import it (it says there is no module with the relevant name). I am using Python 2.7 in Windows 7 (Anaconda distribution, using iPython interpreter).


Detailed summary

I am learning how to upload a package (mymathFoo) at the cheese shop (pyPI), and trying to make sure I can install it once it is uploaded. When I manually paste the package folder into my Lib/site-packages directory, it imports and works fine from my interpreter.

What I have done:

0. Write package

It's a silly little package with an add and subtract module (e.g., add(1,2)). The directory is structured as follows:

\mymathFoo
    __init__.py
    add.py   #adds two numbers
    subtract.py  #subtract two numbers
    setup.py  #shown below
    README.txt  #simple description of package

The __init__.py file is as follows:

from add import add 
from subtract import subtract

1. Register the package at pyPI

Namely, from the command line, I enter:

python setup.py register

Which returns:

running register
running check
Registering mymathFoo to http://pypi.python.org/pypi
Server response (200): OK

Note my setup.py file is:

from distutils.core import setup

setup(name="mymathFoo", 
      version="0.6.2", 
      url="http://mymathfoo.net",
      maintainer='Math Foo',
      maintainer_email='mathfoo@math.org',
      py_modules=['add','subtract'],
      description="Silly little math turd to add/subtract.") 

Note if I change it to py_modules='mymathFoo' I get the same error as below.

2. Upload the package At the command line, I entered:

python setup.py sdist bdist_wininst upload

And the response was:

running sdist
running check
warning: sdist: manifest template 'MANIFEST.in' does not exist (using default fi
le list)

writing manifest file 'MANIFEST'
creating mymathFoo-0.6.3
copying files to mymathFoo-0.6.3...
copying README.txt -> mymathFoo-0.6.3
copying add.py -> mymathFoo-0.6.3
copying setup.py -> mymathFoo-0.6.3
copying subtract.py -> mymathFoo-0.6.3
creating dist
creating 'dist\mymathFoo-0.6.3.zip' and adding 'mymathFoo-0.6.3' to it
adding 'mymathFoo-0.6.3\add.py'
adding 'mymathFoo-0.6.3\PKG-INFO'
adding 'mymathFoo-0.6.3\README.txt'
adding 'mymathFoo-0.6.3\setup.py'
adding 'mymathFoo-0.6.3\subtract.py'
removing 'mymathFoo-0.6.3' (and everything under it)
running bdist_wininst
running build
running build_py
creating build
creating build\lib
copying add.py -> build\lib
copying subtract.py -> build\lib
installing to build\bdist.win-amd64\wininst
running install_lib
creating build\bdist.win-amd64
creating build\bdist.win-amd64\wininst
creating build\bdist.win-amd64\wininst\PURELIB
copying build\lib\add.py -> build\bdist.win-amd64\wininst\PURELIB
copying build\lib\subtract.py -> build\bdist.win-amd64\wininst\PURELIB
running install_egg_info
Writing build\bdist.win-amd64\wininst\PURELIB\mymathFoo-0.6.3-py2.7.egg-info
creating 'c:\users\eric\appdata\local\temp\tmp65xxe2.zip' and adding '.' to it
adding 'PURELIB\add.py'
adding 'PURELIB\mymathFoo-0.6.3-py2.7.egg-info'
adding 'PURELIB\subtract.py'
removing 'build\bdist.win-amd64\wininst' (and everything under it)
running upload
Submitting dist\mymathFoo-0.6.3.zip to http://pypi.python.org/pypi
Server response (200): OK
Submitting dist\mymathFoo-0.6.3.win-amd64.exe to http://pypi.python.org/pypi
Server response (200): OK

Things seem to have worked. So far so good.

3. Install this package locally using pip.

Then I go to install this package with pip at the command line:

pip install mymathFoo

To which I get:

Downloading/unpacking mymathFoo
  Downloading mymathFoo-0.6.3.zip
  Running setup.py (path:c:\users\eric\appdata\local\temp\pip_build_Eric\mymathF
oo\setup.py) egg_info for package mymathFoo
    Installing collected packages: mymathFoo
  Running setup.py install for mymathFoo
    Successfully installed mymathFoo
Cleaning up...

Running the above causes the following directory to be copied into my Lib/site-packages folder:

mymathFoo-0.6.3-py2.7.egg-info

4. Import the package (not)

This is where I run into the problem. I open my iPython interpreter (using Anaconda distribution of Python, Windows 7):

import mymathFoo

I get:

Traceback (most recent call last):

  File "<ipython-input-7-b7486b6a0225>", line 1, in <module>
    import mymathFoo

ImportError: No module named mymathFoo

What am I missing? Why is my poor little module invisible?

Update

Note if I collapse all the files into the root directory (which I ultimately do not want to do), the errors go away. Unfortunately I will often want directories in my root directory, and nothing I have tried based on comments has fixed this.

I'm still looking for an answer, the discussion here looks promising: http://www.scotttorborg.com/python-packaging/index.html# I will work through that and post any solutions I find.

Discussion of related, but not the same, issues

"ImportError: No module named httplib2" even after installation

how to install package with pypi in python 2.7?

Note This is based on some work I am doing with the book Python 101, by Michael Driscoll (it is in draft form right now).

Community
  • 1
  • 1
eric
  • 7,142
  • 12
  • 72
  • 138

2 Answers2

4

Your package does install, just not the way you intended it to:

$ pip install mymathFoo
Downloading/unpacking mymathFoo
  Using download cache from /Users/stu/tmp/pip-cache/http%3A%2F%2Fpypi.counsyl.com%2Froot%2Fpypi%2F%2Bf%2F64ef17a9135f05820c2d96050ce4315d%2FmymathFoo-0.6.3.zip
  Running setup.py egg_info for package mymathFoo

Installing collected packages: mymathFoo
  Running setup.py install for mymathFoo

Successfully installed mymathFoo
Cleaning up...
$ python
Type "help", "copyright", "credits" or "license" for more information.
>>> import add
>>> import subtract
>>> 

Your setup.py says to install two modules, "add" and "subtract", and not to install a package named mymathFoo. Put add.py and subtract.py in a folder named 'mymathFoo', and set py_modules=['mymathFoo'] in setup.py.

Lastly, you can test your packaging without hitting pypi. Just run python setup.py install, and try to import you package.

Stu Gla
  • 1,129
  • 12
  • 16
  • Stu, you are right I was importing individual functions, which was not my goal. Unfortunately, the reason I did that was because when I had my `setup.py` metadata with `py_modules=['mymathFoo']` and install with `pip install mymathFoo --upgrade`, I still get the `ImportError: No module named mymathFoo` even though pip installing the module seemed to work. – eric May 09 '14 at 12:11
  • Stu wrote " Put add.py and subtract.py in a folder named 'mymathFoo', and set py_modules=['mymathFoo'] in setup.py." I tried that and got the same error with trying to import. When I just bundle all the functions into the root folder into a file `mymathFoo.py`, things worked fine (see mymathFoo v 0.6.7 at pyPI), but I'd like to know what I did wrong with multiple files in a folder. – eric May 09 '14 at 17:51
  • Do you have an (empty) `__init__.py` file inside the mymathFoo directory? – Stu Gla May 12 '14 at 20:49
  • If you are just playing around with or testing uploading to pypi use the testing site: https://testpypi.python.org/pypi – rtrwalker Feb 13 '15 at 02:43
2

it works for me:

>>> import mymathFoo
>>> dir(mymathFoo)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']

though your module exports nothing. It looks like you changed your package since @Stu-Gla's answer, as you removed the add.py and subtract.py from sources.

By the way, you do not need to register a dummy package to pypi in order to test it, you can also use pip to install a package locally:

pip install /path/to/sources # path where the setup.py is
zmo
  • 24,463
  • 4
  • 54
  • 90
  • zmo yes I did a few versions. I think the one I posted was version 0.1 I need to go look at this and clarify which ones I'm talking about in my post, and which errors are which. I am working through documentation in a big way right now, and will summarize my discoveries. (Thanks for pointing out the local installation option I hadn't been doing that). – eric May 11 '14 at 13:27