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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With