Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import error on installed package using setup.py

I have a problem with using setup.py to setup a python package. First, I have the following directory setup:

maindir    |- setup.py    |-mymodule          |- __init__.py          |- mainmodule.py          |-subdir              |- __init__.py              |- submodule.py 

i.e. the project directory contains the setup.py and a directory mymodule, which in itself contains two python modules in two directories. The file submodule.py contains just

teststring = "hello world" 

mainmodule.py contains:

from .subdir import submodule mainstring = "42" 

and setup.py contains:

import os from setuptools import setup setup(     name = "mytestmodule",     version = "0.0.1",     description = ("A simple module."),     packages=['mymodule'], ) 

When I do from mymodule import mainmodule with ipython from within sourceTest the behaviour works as expected and I can reference e.g. mainmodule.submodule.teststring which gives me the string hello world.

On the other side, when I install this 'package' using python setup.py install and try to do the same (from within some other directory), I get an import error:

In [1]: from mymodule import mainmodule --------------------------------------------------------------------------- ImportError                               Traceback (most recent call last) /home/alexander/<ipython-input-1-cf4c9bafa487> in <module>() ----> 1 from mymodule import mainmodule  /home/alexander/build/bdist.linux-i686/egg/mymodule/mainmodule.py in <module>()  ImportError: No module named subdir 

I do not see what I have done wrong, as I followed a Getting started tutorial and rules for importing intra-packages. I suppose my mistake is a really tiny one, but I cannot spot it and help is appreciated.

like image 660
Alex Avatar asked Mar 12 '13 17:03

Alex


People also ask

How do I install Python packages from setup py?

Installing Python Packages with Setup.py To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

Does pip install use setup py?

@joeforker, pip uses setup.py behind the scenes.

How do you import a package in python?

Importing module from a package We can import modules from packages using the dot (.) operator. Now, if this module contains a function named select_difficulty() , we must use the full name to reference it. Now we can directly call this function.

What does setup py Sdist do?

(assuming you haven't specified any sdist options in the setup script or config file), sdist creates the archive of the default format for the current platform. The default format is a gzip'ed tar file ( . tar. gz ) on Unix, and ZIP file on Windows.


1 Answers

You have to list all packages in setup, including subpackages:

setup(     name = "mytestmodule",     version = "0.0.1",     description = ("A simple module."),     packages=['mymodule', 'mymodule.subdir'], ) 

Or you can use setuptools's magic function find_packages:

from setuptools import setup, find_packages setup(     name = "mytestmodule",     version = "0.0.1",     description = ("A simple module."),     packages=find_packages(), ) 

This is mentioned here:

If you have sub-packages, they must be explicitly listed in packages, but any entries in package_dir automatically extend to sub-packages. (In other words, the Distutils does not scan your source tree, trying to figure out which directories correspond to Python packages by looking for __init__.py files.)

like image 176
Pavel Anossov Avatar answered Oct 05 '22 05:10

Pavel Anossov