尝试使用pip安装python包时出错。
在解开包后它在某个目录中查找“setup.py”,但在那里找不到它。 setup.py文件实际上是一个目录。
正在寻找:
'virtualenvs/pyling/build/nltk/setup.py'
但它确实在:
virtualenvs/pyling $ ls build/nltk/nltk-2.0b9/
INSTALL.txt javasrc LICENSE.txt nltk PKG-INFO README.txt setup.py
有没有办法让pip知道包中的这个嵌套文件夹结构?
由于
答案 0 :(得分:1)
我发现pip运行的python命令行脚本如下所示:
__file__ = '<path to package>/setup.py'
from setuptools.command import egg_info
def replacement_run(self):
self.mkpath(self.egg_info)
installer = self.distribution.fetch_build_egg
for ep in egg_info.iter_entry_points('egg_info.writers'):
# require=False is the change we're making:
writer = ep.load(require=False)
if writer:
writer(self, ep.name, egg_info.os.path.join(self.egg_info,ep.name))
self.find_sources()
egg_info.egg_info.run = replacement_run
execfile(__file__)
在软件包的顶级目录中,没有setup.py,我放置了一个可以重现这种行为的setup.py,但是chdir到包含“真正的”setup.py的目录并在结束了。
唯一的另一个问题是pip创建了一个目标“pip-egg-info”,它希望填充它,需要在“真实”setup.py的目录中创建一个符号链接。
新的顶级setup.py如下所示:
#! /usr/bin/env python
from os import chdir, symlink, getcwd
from os.path import join, basename, exists
filename = basename(__file__)
chdir("python")
setupdir = getcwd()
egginfo = "pip-egg-info"
if not exists(egginfo) and exists(join("..", egginfo)):
symlink(join("..", egginfo), egginfo)
__file__ = join(setupdir, filename)
from setuptools.command import egg_info
def replacement_run(self):
self.mkpath(self.egg_info)
installer = self.distribution.fetch_build_egg
for ep in egg_info.iter_entry_points('egg_info.writers'):
# require=False is the change we're making:
writer = ep.load(require=False)
if writer:
writer(self, ep.name, egg_info.os.path.join(self.egg_info,ep.name))
self.find_sources()
egg_info.egg_info.run = replacement_run
execfile(__file__)
它可能很脆弱,但应该继续工作,直到pip更改它生成的命令行python代码
答案 1 :(得分:0)
我添加了一个小函数,并在pip / req.py文件中的setup_py的属性定义中调用它。
这似乎使它按照我期望的方式工作,即如果setup.py文件只有一层更深。希望在捆绑和冻结时没有任何东西被破坏......
import os
def get_setup_path(in_path):
print "starting path is %s"%in_path
fileList=os.listdir(in_path)
if fileList.__contains__("setup.py"):
print "setup.py is indeed there"
return in_path
for f in fileList:
f=os.path.join(in_path,f)
if os.path.isdir(f):
contents=os.listdir(f)
if contents.__contains__("setup.py"):
out_path=os.path.join(in_path,f)
return out_path
print "could not find setup.py by looking one level deeper"
return in_path
然后在req.py
中调用它(around line 195 in req.py)
@property
def setup_py(self):
self.source_dir= get_setup_path(self.source_dir)
return os.path.join(self.source_dir, 'setup.py')
答案 2 :(得分:0)
setup.py
应位于顶级目录中。或
pip
指向包含setup.py
文件的目录。答案 3 :(得分:0)
从不同于setup.py所在的目录调用pip时,我已经遇到了这个问题。解决方案是将以下内容添加到setup.py中:
导入操作系统
os.chdir(os.path.dirname(os.path.abspath则(__file__
)))
也许你从virtualenvs / pyling / build / nltk /调用pip它应该是virtualenvs / pyling / build / nltk / nltk-2.0b9 /?然后加上我做的。
答案 4 :(得分:0)
我有类似的问题,并放弃使用pip的默认网址。但是,如果您只想安装nltk,这对我有用:
pip install -E <virtualenv-dir> PyYAML #if you have not done so
pip install -E <virtualenv-dir> --no-index --find-links=http://nltk.googlecode.com/ nltk