Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right way to set python package with sub-packages

Tags:

I am trying to set a package with sub-packages in python. Here is the tree structure that I have at the moment:

myPackage ├── __init__.py ├── mySubPackage1 │   ├── foo2.py │   ├── foo.py │   └── __init__.py ├── mySubPackage2 │   ├── bar2.py │   ├── bar.py │   └── __init__.py └── setup.py 

All __init__.py are empty. Here is my setup.py:

from distutils.core import setup  if __name__ == "__main__":     setup(         name='myPackage',         package_dir = {             'mySubPackage1': 'mySubPackage1',             'mySubPackage2': 'mySubPackage2'},         packages=['mySubPackage1', 'mySubPackage2'],     ) 

The problem is that, when I run python setup.py install from myPackage, the sub packages are installed into dist-packages:

/usr/local/lib/python2.7/dist-packages/mySubPackage1 /usr/local/lib/python2.7/dist-packages/mySubPackage2 

I guess the problem is my setup.py, but I don't know how to fix it? Should it be in the parent directory of myPackage? If so, then how does it work when I pack the package into a zip using python setup.py sdist?

like image 637
Dror Avatar asked Oct 23 '14 12:10

Dror


People also ask

Can packages have sub-packages?

Packages can be nested to contain subpackages, and that can be done to an arbitrary depth.

What are namespace packages in Python?

In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads.

How to make packages in Python?

Packages are of two types, namely First, we need to think of a way to structure our code, so that others can access our code functionalities. In Python, to make a package, we need to add an __init__.py to the directory. Here, we are going to make a package called test_package. Let’s write the __init__.py

What is the structure of a simple Python package with two modules?

The structure of a simple Python package with two modules is as follows: . As mentioned, packages can contain sub-packages. We can use sub-packages to organize our code further. I’ll show you how to do that in more detail in one of the sections below. Let’s first take a look at the structure of a package with sub-packages: .

What is the difference between a package and a directory?

To understand this better, the following image illustrates the structure of Python packages. Although a package is also a directory, the main distinction between these two is that the package contains __init__.py file and the directory doesn’t. The __init__.py file makes an ordinary directory into a Python package.

Why are Python packages so difficult to install and use?

However, when it comes to installing and using these packages, newcomers often find themselves running into issues with missing permissions, incompatible library dependencies, and installations that break in surprising ways. The Zen of Python states: "There should be one—and preferably only one—obvious way to do it."


1 Answers

Just use setuptools instead of distutils, it has find_packages exactly for that purpose:

from setuptools import setup, find_packages  setup(     name='myPackage',     packages=find_packages(), ) 
like image 138
Max Malysh Avatar answered Sep 20 '22 08:09

Max Malysh