Python setup.py 错误:'install_requires' 必须是字符串或字符串列表

0 投票
1 回答
61 浏览
提问于 2025-04-14 18:34

我正在尝试构建这个Transformer Grammars的代码,链接在这里:https://github.com/google-deepmind/transformer_grammars/tree/main

我在使用Google Cloud的深度学习虚拟机,N1实例,运行的是TensorFlow 2.10(CUDA 11.3,Python 3.7)。

这是我正在尝试运行的install.sh文件的内容:

set verbose
set -o errexit

rm -rf .dependencies
mkdir .dependencies
cd .dependencies
git clone -b 20220623.1 https://github.com/abseil/abseil-cpp.git
git clone -b 3.4.0 https://gitlab.com/libeigen/eigen.git
git clone -b v2.10.2 https://github.com/pybind/pybind11.git

sudo apt-get install cmake build-essential pkg-config libgoogle-perftools-dev
git clone -b v0.1.97 https://github.com/google/sentencepiece.git
cd sentencepiece
mkdir build
cd build
make -j
sudo make install
sudo ldconfig -v

cd ../../..
pip install --require-hashes -r requirements.txt
pip install -e . --no-deps --no-index

rm -rf .dependencies

错误信息是:

error in transformer_grammars setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers; Invalid requirement, parse error at "'\\'"

在setup.py文件中的setup()命令里,关于install_requires的代码是:

install_requires=dependencies

dependencies是这样获取的:

with open("requirements.txt", "r") as f:
     dependencies = list(map(lambda x: x.strip(), f.readlines()))

值得注意的是,这个信息在requirements.txt的底部:

# WARNING: The following packages were not pinned, but pip requires them to be
# pinned when the requirements file includes hashes. Consider using the --allow-unsafe flag.
# setuptools

我其实不太明白这是什么意思,但我猜这可能和其他我看到的类似错误有关,因为那些错误也涉及到了setuptools。

我尝试过的一些方法,但都没有成功:

  • 升级pip
  • 升级setuptools
  • 降级到旧版本的setuptools(39.1.0)
  • 联系DeepMind的相关人员

非常感谢任何帮助。

1 个回答

0

这个问题很可能是因为你的requirements.txt文件里有注释或者其他多余的信息。确保这个文件的格式是正确的,然后再试试这个:

def parse_req_line(s: str) -> str:
    s = s.strip().replace("\n", "")
    return "" if len(s) < 2 or s.startswith("#") else s

with open("requirements.txt", "r") as f:
     dependencies = [d for l in f.readlines() if (d := parse_req_line(l))]

撰写回答