Search code examples
pythonpython-3.xsetuptoolspython-wheel

Python3 out of source build


What I want

Put all build output generated (build, dist, my_package.egg_info, etc) by python setup.py into a specific directory.

Project structure

awesome_pkg/
    __init__.py
    mod.py
my_fancy_app/
   __init__.py
   __main__.py
target/
requirements.txt
setup.py

__main__.py:

def main():
    print("Hello virtual environment")

if __name__ == '__main__':
    main()

setup.py:

import setuptools

setuptools.setup(
        name = "my_fancy_app",
        version="0.0.1",
        author="Me Myself",
        author_email="[email protected]",
        description="Some description",
        long_description="Some long description",
        packages=setuptools.find_packages(),
        entry_points={
            'console_scripts': [ 'hlpth = my_fancy_app.__main__:main' ]
        },
        classifiers=[
            "Programming Language :: Python :: 3.7",
            "License :: OSI Approved :: MIT License",
            "Operating System :: OS Independent",
        ],
        python_requires='>=3.7'
)

__init__.py is an empty script


What I tried

I tried to simply run setup.py with a specific working directory:

cd target
python3.7 ../setup.py bdist_wheel

The problem

The problem is it does not really package anything:

$ python3.7 ../setup.py bdist_wheel 
running bdist_wheel                                                                                                                                                                                                                    
running build                                                                                                                                                                                                                          
installing to build/bdist.linux-x86_64/wheel                                                                                                                                                                                           
running install                                                                                                                                                                                                                        
running install_egg_info                                                                                                                                                                                                               
running egg_info                                                                                                                                                                                                                       
creating my_fancy_app.egg-info                                                                                                                                                                                                         
writing my_fancy_app.egg-info/PKG-INFO                                                                                                                                                                                                 
writing dependency_links to my_fancy_app.egg-info/dependency_links.txt                                                                                                                                                                 
writing entry points to my_fancy_app.egg-info/entry_points.txt                                                                                                                                                                         
writing top-level names to my_fancy_app.egg-info/top_level.txt                                                                                                                                                                         
writing manifest file 'my_fancy_app.egg-info/SOURCES.txt'                                                                                                                                                                              
reading manifest file 'my_fancy_app.egg-info/SOURCES.txt'                                                                                                                                                                              
writing manifest file 'my_fancy_app.egg-info/SOURCES.txt'                                                                                                                                                                              
Copying my_fancy_app.egg-info to build/bdist.linux-x86_64/wheel/my_fancy_app-0.0.1-py3.7.egg-info                                                                                                                                      
running install_scripts                                                                                                                                                                                                                
creating build/bdist.linux-x86_64/wheel/my_fancy_app-0.0.1.dist-info/WHEEL                                                                                                                                                             
creating 'dist/my_fancy_app-0.0.1-py3-none-any.whl' and adding 'build/bdist.linux-x86_64/wheel' to it                                                                                                                                  
adding 'my_fancy_app-0.0.1.dist-info/METADATA'                                                                                                                                                                                         
adding 'my_fancy_app-0.0.1.dist-info/WHEEL'                                                                                                                                                                                            
adding 'my_fancy_app-0.0.1.dist-info/entry_points.txt'                                                                                                                                                                                 
adding 'my_fancy_app-0.0.1.dist-info/top_level.txt'                                                                                                                                                                                    
adding 'my_fancy_app-0.0.1.dist-info/RECORD'                                                                                                                                                                                           
removing build/bdist.linux-x86_64/wheel                                                                                                                                                                                                

But running it from the source root directory works perfectly fine python3.7 setup.py bdist_wheel and both of the packages are bundled into the wheel archive.

Is there a way to do such out of source build?


Solution

  • You will have to play around until you find the right combination of setuptools commands and options for your exact use case. For example I have had some success with the following combination for a bdist_wheel:

    python3 setup.py egg_info --egg-base target build --build-base target/build bdist_wheel --dist-dir target/dist