简体   繁体   中英

setup.py in python: how to install subpackages?

Consider the following package structure:

src/
   /__init__.py
   bar.py
   /foo
       /__init__.py
       foobar.py

I want to write a setup.py such that if I do:

pip install -e .

from the root directory, all the following will work:

import src
import src.foo
import foo

I am able to achieve the first two easily using eg find_namespace_packages, but I have tried various combos of find_namespace_packages(), find_packages(), package_dir, to also be able to import the subpackage foo without specifying the prefix (ie src.foo) without success. How do I do this?

Perhaps I'm wrong, but I'm fairly sure you can't with the way you structure/package your repo. Python will look in src and each __init__.py allows for it to be indexed and thus it's consider part of the same package. Plus, there are potential namespace conflicts. What if instead of foo you had csv ? That would break the default csv library for python. By specifying src.foo it's explicit where foo is defined.

The only way I can think is to have your code in two separate repository with foo available on PyPI (ie, pip install foo ). After adding it to your requirements.txt Then you can do something like:

import foo

in src/__init__.py . This would allow for you to have: src.foo and foo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM