Possible to create python sdist from different directory?

3k views Asked by At

Want to create python source distribution by running python setup.py sdist from a directory outside of the one I want to package up. Can't seem to find a way to do this. I have a script that generates a setup.py and MANIFEST.in dynamically, and I'd like to tell python to use those files to create an sdist of the source in a different directory "over there".

What I'm doing is creating a script that lets a user create an sdist w/o any setup.py etc. They just say "package up this directory and everything under it". So I generate a setup.py and MANIFEST.in (with recursive-include * to grab all files) in a python tempfile.mkdtemp directory (in an unrelated file path like /tmp/whatever) that I can clean up afterwards...but I can't seem to use those to package their directory. I don't want to create those files in their source dir.

2

There are 2 answers

3
Thomas Fauskanger On

You can use setuptools's, --dist-dir=DIR / -d DIR option to specify where the otherwise default dist/-folder is made. In other words, this changes the output directory.

E.g.:

python setup.py sdist -d /tmp/whatever

If you are using distutils.core: Instead of using from distutils.core import setup you can use from setuptools import setup.

In order to define where the source directories come from, I think you can add the directory to sys.path and then setup() will discover the content files automatically:

import sys
from os import path
# ...

# Add other folders to sys.path
sys.path.append('/tmp/whatever')
sys.path.append(path.join(path.abspath('..'), 'some', 'folder'))
0
andrewwong09 On

Sort of a hack, but this worked for me. Right before running setup(), use os.chdir() to change the directory to that of the base path where setup.py would normally run. To specify where the distribution packages go, I use the arguments to setup.py, specifically:

python setup.py sdist --formats=gztar -d 'directory_for_the_distribution' egg_info --egg-base 'directory_for_the_egg_info'

Thus you can run setuptools from a directory other than at the base of the package directories and the distribution and temp egg directories go wherever you want.