hosselausso
hosselausso

Reputation: 1087

Install modules with setup.py and setup.cfg

I have the following directory structure:

/modules/
/modules/setup.py
/modules/setup.cfg

/modules/module1/
/modules/module1/__init__.py
/modules/module1/tool1/__init__.py
/modules/module1/tool1/tool1.py

/modules/module2/
/modules/module2/__init__.py
/modules/module2/tool2/__init__.py
/modules/module2/tool2/tool2.py
/modules/module2/tool3/__init__.py
/modules/module2/tool3/tool3.py

And I want to install these modules using setup.py and setup.cfg and import them later on like this:

import my_project.module1.tool1 
import my_project.module2.tool2
import my_project.module2.tool3 

These are my installation files:

setup.py

import setuptools

setuptools.setup(
    setup_requires=['paramiko>=2.0.1'],
    paramiko=True)

setup.cfg

[metadata]
name = my_project
summary = my project modules

[files]
packages =
    module1
    module2

It fails when I try to install the packages:

/modules# pip install -e .
Obtaining file:///modules
Installing collected packages: UNKNOWN
  Found existing installation: UNKNOWN 0.0.0
    Can't uninstall 'UNKNOWN'. No files were found to uninstall.
  Running setup.py develop for UNKNOWN
Successfully installed UNKNOWN

Upvotes: 12

Views: 1752

Answers (2)

Noorvh
Noorvh

Reputation: 44

In order to import your modules the way you want, you first need to make sure the directory structure is correct, otherwise importing the module tools will fail anyway. It should look like this:

│-- setup.py
│-- setup.cfg
│
│-- my_project/
│   │-- __init__.py  # Pretty important: Makes "my_project" a package
│   │
│   ├── module1/
│   │   │-- __init__.py
│   │   ├── tool1/
│   │   │   │-- __init__.py
│   │   │   │-- tool1.py
│   │
│   ├── module2/
│       │-- __init__.py
│       ├── tool2/
│       │   │-- __init__.py
│       │   │-- tool2.py
│       ├── tool3/
│           │-- __init__.py
│           │-- tool3.py

Then it you also need to fix setup.py like the other commenter mentioned

from setuptools import setup, find_packages

setup(
    name="my_project",
    packages=find_packages(),
    install_requires=['paramiko>=2.0.1'],
)

Let me know if this works for you. With this you should be able to import the packages in the way you described above.

Upvotes: 1

Marcin Cuprjak
Marcin Cuprjak

Reputation: 694

Try using the find_packages function:

from setuptools import setup, find_packages
...
setup(
    ...
    packages=find_packages(),
    ...

Upvotes: 0

Related Questions