TransWikia.com

setuptools - bundle package from framework from relative path

Stack Overflow Asked on December 25, 2021

With a project setup as follows:

---------------------
root
   FrameworkPackage1
      __init__.py
     sourcefile1.py

   FrameworkPackage2
      __init__.py
      sourcefile2.py
   apps
       Project
           src
               MyApp
                   __init__.py
                   __main__.py
           setup.py
           README.md
---------------------

When I’m creating the setup.py, from what I understand, I use package_dir to set the location of these packages.

---------------------
    packages=['MyApp', 'FrameworkPackage1', 'FrameworkPackage2'],
    package_dir={'': 'src',
                 'FrameworkPackage1': '../../FrameworkPackage1',
                 'FrameworkPackage2': '../../FrameworkPackage2'}
---------------------

So this correctly builds a package with all the required files. However, when I try to install, it fails, and if I just try to untar/gz the package file it puts FrameworkPackage1/2 in the "../../.." dir from where the unzip happens.

Ideally I’d like the package to work as follows and install from pip so I could run the following:

import MyApp as ma
import FrameworkPackage1 as fp1
import FrameworkPackage2 as fp2

print(ma.Function())
print(fp1.OtherFunction())
print(fp2.OtherFunction())

Is there a way to set the frameworks to be found in "../../../" but install into the root of the distribution?

One Answer

Firstly, as @a_guest suggested, shouldn't the package_dir look like this?

packages=['MyApp', 'FrameworkPackage1', 'FrameworkPackage2'],
package_dir={'': 'src',
             'FrameworkPackage1': '../../FrameworkPackage1',
             'FrameworkPackage2': '../../FrameworkPackage2'}

Alternatively, you could try adding a __init__.py to the root folder so that it is recognized as a python folder (based on this question)

Secondly, instead of using this bundled structure for your package, you could either:

  • If the Framework packages are used elsewhere: Treat each package separately. This would allow you to evolve them separately, and add them to your MyApp by simply including them in the requirements.txt (or equivalents). A con of this is that each one would have its own setup.py, but this is a much better packaging practice.
  • If the Framework packages are not used elsewhere (or you just want your local copy): Switch to a project setup with the setup.py directly in the main folder ( package_dir={'': 'src', 'FrameworkPackage1': 'src', 'FrameworkPackage2': 'src'}, with a structure looking like:
---------------------
...
       Project
           src
               MyApp
                   __init__.py
                   __main__.py
               FrameworkPackage1
                   __init__.py
                   sourcefile1.py
               FrameworkPackage2
                   __init__.py
                   sourcefile2.py
           setup.py
           README.md
---------------------

Answered by ibarrond on December 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP