Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python packaging: subdirectories not installed

I have a Python project with the layout

setup.py
foobar/
    __init__.py
    foo.py
    bar/
        __init__.py

Where the foobar/__init__.py reads

from . import foo
from . import bar

and setup.py

from setuptools import setup

setup(
    name='foobar',
    version='0.0.1',
    packages=['foobar'],
    )

When doing import foobar from the source directory, it all works as expected. However, when installing the package via pip install ., the subfolder bar/ is not installed, leading to the import error

ImportError: cannot import name bar

Any hints?

like image 969
Nico Schlömer Avatar asked Apr 06 '17 11:04

Nico Schlömer


People also ask

How do I manually install a Python package?

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.

Where are Python packages installed?

When a package is installed globally, it's made available to all users that log into the system. Typically, that means Python and all packages will get installed to a directory under /usr/local/bin/ for a Unix-based system, or \Program Files\ for Windows.

Does Python search subdirectories for file?

Use glob. glob() to search for specific files in subdirectories in Python. Call glob. glob(pathname, recursive=True) with pathname as a path to a directory and recursive as True to enable recursively searching through existing subdirectories.

What is Package_dir in setup py?

package_dir = {'': 'lib'} in your setup script. The keys to this dictionary are package names, and an empty package name stands for the root package. The values are directory names relative to your distribution root.


1 Answers

Apparently to include subpackages, you need find_packages():

from setuptools import setup, find_packages

setup(
    name='foobar',
    version='0.0.1',
    packages=find_packages()
    )

This is recommended​ in the setuptools docs as well.

like image 134
Nico Schlömer Avatar answered Oct 08 '22 01:10

Nico Schlömer