Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

python - How can I get my setup.py to use a relative path to my files?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...