I'm trying to build a Python distribution with distutils
. Unfortunately, my directory structure looks like this:
/code /mypackage __init__.py file1.py file2.py /subpackage __init__.py /build setup.py
Here's my setup.py
file:
from distutils.core import setup
setup(
name = 'MyPackage',
description = 'This is my package',
packages = ['mypackage', 'mypackage.subpackage'],
package_dir = { 'mypackage' : '../mypackage' },
version = '1',
url = 'http://www.mypackage.org/',
author = 'Me',
author_email = '[email protected]',
)
When I run python setup.py sdist
it correctly generates the manifest file, but doesn't include my source files in the distribution. Apparently, it creates a directory to contain the source files (i.e. mypackage1
) then copies each of the source files to mypackage1/../mypackage
which puts them outside of the distribution.
How can I correct this, without forcing my directory structure to conform to what distutils
expects?
A relative path starts with / , ./ or ../ . To get a relative path in Python you first have to find the location of the working directory where the script or module is stored. Then from that location, you get the relative path to the file want.
Opening a File with Relative Path In the relative path, it will look for a file into the directory where this script is running. # Opening the file with relative path try: fp = open("sample. txt", "r") print(fp. read()) fp.
To use it, you just pass a path or filename into a new Path() object using forward slashes and it handles the rest: Notice two things here: You should use forward slashes with pathlib functions. The Path() object will convert forward slashes into the correct kind of slash for the current operating system.
By default, Django settings include a BASE_DIR value, which is an absolute path to the directory containing manage.py (usually one level higher than the settings.py file or two levels higher than settings/_base.py ). Then, we set all of the paths relative to BASE_DIR using the os. path. join() function.
What directory structure do you want inside of the distribution archive file? The same as your existing structure?
You could package everything one directory higher (code
in your example) with this modified setup.py:
from distutils.core import setup
setup(
name = 'MyPackage',
description = 'This is my package',
packages = ['mypackage', 'mypackage.subpackage'],
version = '1',
url = 'http://www.mypackage.org/',
author = 'Me',
author_email = '[email protected]',
script_name = './build/setup.py',
data_files = ['./build/setup.py']
)
You'd run this (in the code
directory):
python build/setup.py sdist
Or, if you want to keep dist
inside of build:
python build/setup.py sdist --dist-dir build/dist
I like the directory structure you're trying for. I've never thought setup.py
was special enough to warrant being in the root code folder. But like it or not, I think that's where users of your distribution will expect it to be. So it's no surprise that you have to trick distutils to do something else. The data_files
parameter is a hack to get your setup.py into the distribution in the same place you've located it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With