python 如何让我的 setup.py 使用我的文件的相对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/812242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get my setup.py to use a relative path to my files?
提问by Chris B.
I'm trying to build a Python distribution with distutils
. Unfortunately, my directory structure looks like this:
我正在尝试使用distutils
. 不幸的是,我的目录结构是这样的:
/code /mypackage __init__.py file1.py file2.py /subpackage __init__.py /build setup.py
Here's my setup.py
file:
这是我的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 = '[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 outsideof the distribution.
当我python setup.py sdist
正确运行它时会生成清单文件,但不会在分发中包含我的源文件。显然,它创建了一个目录来包含源文件(即mypackage1
),然后将每个源文件复制到mypackage1/../mypackage
其中,将它们置于发行版之外。
How can I correct this, without forcing my directory structure to conform to what distutils
expects?
如何在不强制我的目录结构符合distutils
预期的情况下纠正此问题?
回答by Jon-Eric
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:
您可以code
使用此修改后的 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):
你会运行这个(在code
目录中):
python build/setup.py sdist
Or, if you want to keep dist
inside of build:
或者,如果您想保留dist
在构建中:
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.
我喜欢你正在尝试的目录结构。我从来没有想过setup.py
特别到可以保证在根代码文件夹中。但不管你喜不喜欢,我认为这就是你的发行版用户所期望的。因此,您必须欺骗 distutils 做其他事情也就不足为奇了。该data_files
参数是一个技巧,可以将您的 setup.py 放入您所在位置的发行版中。
回答by Ken Arnold
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.
等等。不确定这是否有效,但应该很容易尝试。
回答by Evan Plaice
Run setup.py from the root folder of the project
从项目的根文件夹运行 setup.py
In your case, place setup.py in code/
在您的情况下,将 setup.py 放在 code/ 中
code/ should also include:
代码/还应包括:
- LICENSE.txt
- README.txt
- INSTALL.txt
- TODO.txt
- CHANGELOG.txt
- 许可证.txt
- 自述文件
- 安装文件
- 待办事项.txt
- 变更日志.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
当您运行“setup.py sdist”时,它应该自动生成一个清单,包括: - 在 py_modules 和/或包中指定的任何文件 - setup.py - README.txt
To add more files just hand-edit the MANIFEST file to include whatever other files your project needs.
要添加更多文件,只需手动编辑 MANIFEST 文件以包含项目所需的任何其他文件。
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.
注意:我没有将 MANIFEST 置于版本控制之下,因此您不会在那里找到它。
回答by kutschkem
Also a lame workaround, but a junction/link of the package directory inside of the build project should work.
也是一个蹩脚的解决方法,但是构建项目内的包目录的连接/链接应该可以工作。
回答by easel
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.
有点蹩脚的解决方法,但我可能只使用将 ./mypackage 同步到 ./build/mypackage 的 Makefile,然后在 ./build 中使用通常的 distutils 语法。事实是,distutils 期望将 setup.py 解压到 sdist 的根目录中并在其中放置代码,因此您将花费大量时间说服它做其他事情。
You can always nuke the copy when you make clean so you don't have to mess up your vcs.
当您清理时,您可以随时取消副本,这样您就不必弄乱您的 vcs。