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

前端 未结 5 1089
孤街浪徒
孤街浪徒 2021-01-04 04:43

I\'m trying to build a Python distribution with distutils. Unfortunately, my directory structure looks like this:

/code
    /mypackage
        __ini         


        
相关标签:
5条回答
  • 2021-01-04 04:49

    A sorta lame workaround but I'd probably just use a Makefile that rsynced ./mypackage to ./build/mypackage and then use the usual distutils syntax from inside ./build. Fact is, distutils expects to unpack setup.py into the root of the sdist and have code under there, so you're going to have a devil of time convincing it to do otherwise.

    You can always nuke the copy when you make clean so you don't have to mess up your vcs.

    0 讨论(0)
  • 2021-01-04 04:57

    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 = 'me@here.com',
        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.

    0 讨论(0)
  • 2021-01-04 05:06

    Also a lame workaround, but a junction/link of the package directory inside of the build project should work.

    0 讨论(0)
  • Run setup.py from the root folder of the project

    In your case, place setup.py in code/

    code/ should also include:

    • LICENSE.txt
    • README.txt
    • INSTALL.txt
    • TODO.txt
    • CHANGELOG.txt

    The when you run "setup.py sdist' it should auto-gen a MANIFEST including: - any files specified in py_modules and/or packages - setup.py - README.txt

    To add more files just hand-edit the MANIFEST file to include whatever other files your project needs.

    For a somewhat decent explanation of this read this.

    To see a working example checkout my project.

    Note: I don't put the MANIFEST under version control so you won't find it there.

    0 讨论(0)
  • 2021-01-04 05:15

    Have it change to the parent directory first, perhaps?

    import os
    os.chdir(os.pardir)
    
    from distutils.core import setup
    

    etc.

    Or if you might be running it from anywhere (this is overkill, but...):

    import os.path
    my_path = os.path.abspath(__file__)
    os.chdir(os.normpath(os.path.join(my_path, os.pardir)))
    

    etc. Not sure this works, but should be easy to try.

    0 讨论(0)
提交回复
热议问题