RichardS
RichardS

Reputation: 61

Setuptools: Import from 'project' with setup.py in source folder

I am trying to create a setup.py for an existing project. The project has a directory structure that I cannot change. I need my setup.py to be in the same folder as my project source files.

Sample 1, directory structure.

MyModule
├── __init__.py
├── MyApp.ini
├── MyApp.py
├── setup.py
└── foo.py

This is my stetup.py

from setuptools import setup, find_packages
packages = find_packages(exclude=['ez_setup', 'tests', 'tests.*'])

console_script = list()
console_script.append('MyApp = MyApp:main')

py_modules = list()
py_modules.append('MyApp')

other_files = list()
other_files.append('MyApp.ini')

module_name = "MyModule"

mysetup = setup(name=module_name,
                py_modules=py_modules,
                version="1.0.0",
                packages=packages,
                package_dir={module_name: module_name},
                package_data={module_name: other_files},
                include_package_data=True,
                entry_points={'console_scripts': console_script, },
                zip_safe=False,
                python_requires='>=2.7,<=3.0',
                )

After installing MyModule via 'python setup install'. I cannot import from MyModule. 'from MyModule import MyApp' does not work. I can import directly. 'import MyApp' works. The problems is 'import foo' works as well. I have multiple projects with different foo.py.

Sample 2:

If I could change the directory structure as shown below. The install works correctly.

MyModule
├── MyModule
│   ├── foo.py
│   ├── __init__.py
│   ├── MyApp.ini
│   └── MyApp.py
└── setup.py

Is there a way to get sample 1, to install the way sample 2 does?

Upvotes: 3

Views: 1310

Answers (1)

RichardS
RichardS

Reputation: 61

I was able to answer my own question. It can be done by setting package_dir up one level as shown below. I had to use data_files rather than package_data to add my support files.

Limitation: The setup script, setup.py, is installed as part of the egg. I tried to excluding it, but it gets installed anyway.

from setuptools import setup, find_packages
packages = find_packages(exclude=['ez_setup', 'tests', 'tests.*'])

console_script = list()
console_script.append('MyApp = MyModule.MyApp:main')

packages.append("MyModule")

setup(name="MyModule",
      version="1.0.0",
      packages=packages,
      package_dir={"MyModule": "../MyModule"},
      data_files=[('MyModule', ['MyApp.ini'])],
      include_package_data=True,
      entry_points={'console_scripts': console_script, },
      zip_safe=False,
      python_requires='>=2.7,<=3.0',
      )

Upvotes: 3

Related Questions