Search code examples
pythonpipimporterrordistutils

Can't import my own module distributed with distutils


I'd like to package my application to share it between several projects. My setup.py looks like this:

# -*- coding: utf-8 -*-
from distutils.core import setup

setup(
    name='foo_bar',
    version='1.0',
    py_modules=['foo_bar'],
    install_requires=[
        'bitstring==3.1.5',
        'pytz==2016.4',
        'wheel==0.24.0', ]
)

Then I run command

python setup.py sdist

that creates tar.gz file for me

I'm having trouble with using my foo_bar application. I'm installing it into separate virtualenv via pip

pip install dist/foo_bar.tar.gz

and output of pip freeze suggests that it is installed

foo-bar==1.0
bitstring==3.1.5
pytz==2016.4
wheel==0.24.0

When I try to import this module in python console

import foo_bar

I get ImportError: No module named 'foo_bar'

What I'm missing?

Edit:

My file structure looks like this:

foo_bar
├── dist
│   └── foo_bar-1.0.tar.gz
├── __init__.py
├── MANIFEST
├── bar.py
├── requirements.txt
├── setup.py

Solution

  • Could you provide your directory structure? Are you in the same virtualenv when you're trying to import your module? Why are you using py_modules and not packages?

    Moreover, you're trying to import foo_bar, however there is no foo_bar.py in your package! Try to rename bar.py to that name.

    Note: I've never used packages this way. Usually you create a package (directory with __init__.py file) on the same level as setup.py is. This means, that you would create another foo_bar dir in you project and there you would place bar.py. Then you would import it like import foo_bar.bar.