如何获取setup.py以使用文件的相对路径? [英] How can I get my setup.py to use a relative path to my files?

查看:294
本文介绍了如何获取setup.py以使用文件的相对路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 distutils 构建Python发行版。不幸的是,我的目录结构如下所示:

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

这是我的 setup.py 文件:

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

当我运行 python setup.py sdist 时,它会正确生成清单文件,但不包含我的源代码文件中的分布。显然,它创建了一个包含源文件的目录(即 mypackage1 ),然后将每个源文件复制到 mypackage1 /../ mypackage 会将它们放在分布的外部中。

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.

如何纠正此问题,而不必强制目录结构与内容保持一致 distutils 期望吗?

How can I correct this, without forcing my directory structure to conform to what distutils expects?

推荐答案

分发存档文件?与您现有的结构相同吗?

What directory structure do you want inside of the distribution archive file? The same as your existing structure?

您可以将所有内容打包成一个目录(在示例中为 code )修改后的setup.py:

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

或者,如果要保留 dist 在build内部:

Or, if you want to keep dist inside of build:

python build/setup.py sdist --dist-dir build/dist

我喜欢您尝试的目录结构。我从没想到 setup.py 如此特别,足以保证它位于根代码文件夹中。但不管您喜不喜欢,我认为这是您的发行版用户所期望的。因此,您必须欺骗distutils做其他事情也就不足为奇了。 data_files 参数是一种黑客程序,可将setup.py放入与您位于同一位置的发行版中。

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.

这篇关于如何获取setup.py以使用文件的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆