【Python】pip导入requirements.txt报错 CondaValueError: could not parse 'alabaster @ file:///home/ktietz/
最近,在使用多台电脑进行数据运算时,各电脑的环境部署不同,有的直接使用pip方式,有的使用anaconda的虚拟环境
使用:
pip freeze > requirements.txt
pip install -r requirements.txt
出现如下报错:
CondaValueError: could not parse ‘alabaster @ file:///home/ktietz/src/ci/alabaster_1611921544520/work’ in: C:\Users\
打开 requirements.txt 文件发现如下,出现许多的 @ file:形式。
查阅知:
一些包后面跟着 @ file:///… 这样的路径,这表示这些包是以“editable”模式安装的,也就是说它们是从本地文件系统中的某个位置直接安装的,而不是从 PyPI 或其他远程源安装的。这通常发生在以下几种情况:
开发模式安装:当你使用 pip install -e 命令安装一个包时,pip 会以“editable”模式安装该包。这意味着包的源代码会被直接链接到 Python 环境中,任何对源代码的修改都会立即反映在使用该包的环境中。
从本地目录安装:如果你从本地目录(如使用 pip install .)安装一个包,而没有先打包成 wheel 或 sdist,pip 也会以“editable”模式安装它。
构建和安装:有时候,在构建和安装一个包的过程中,如果直接从构建目录安装,也可能导致这种情况。
alabaster @ file:///home/ktietz/src/ci/alabaster_1611921544520/work
alembic==1.13.1
anaconda-client==1.7.2
anaconda-navigator==2.0.3
anaconda-project @ file:///tmp/build/80754af9/anaconda-project_1610472525955/work
anyio @ file:///C:/ci/anyio_1620153418380/work/dist
appdirs==1.4.4
argh==0.26.2
argon2-cffi @ file:///C:/ci/argon2-cffi_1613037959010/work
asn1crypto @ file:///tmp/build/80754af9/asn1crypto_1596577642040/work
astroid @ file:///C:/ci/astroid_1613501047216/work
astropy @ file:///C:/ci/astropy_1617745647203/work
async-generator @ file:///home/ktietz/src/ci/async_generator_1611927993394/work
atomicwrites==1.4.0
attrs @ file:///tmp/build/80754af9/attrs_1604765588209/work
autopep8 @ file:///tmp/build/80754af9/autopep8_1615918855173/work
Babel @ file:///tmp/build/80754af9/babel_1607110387436/work
backcall @ file:///home/ktietz/src/ci/backcall_1611930011877/work
backports.functools-lru-cache @ file:///tmp/build/80754af9/backports.functools_lru_cache_1618170165463/work
backports.shutil-get-terminal-size @ file:///tmp/build/80754af9/backports.shutil_get_terminal_size_1608222128777/work
backports.tempfile @ file:///home/linux1/recipes/ci/backports.tempfile_1610991236607/work
backports.weakref==1.0.post1
bcrypt @ file:///C:/ci/bcrypt_1597936263757/work
beautifulsoup4 @ file:///home/linux1/recipes/ci/beautifulsoup4_1610988766420/work
bitarray @ file:///C:/ci/bitarray_1618435038389/work
bkcharts==0.2
black==19.10b0
解决方法
方法一:使用conda命令导出
了解得知:电脑使用anaconda,但是没有创建虚拟环境,导致上述情况。
conda list -e > requirements.txt # 导出
conda install --yes --file requirements.txt # 安装
方法二:手动移除 @ file:///… 部分
# 假设你的requirements.txt文件路径是'requirements.txt'
input_file_path = 'requirements.txt'
output_file_path = 'clean_requirements.txt'
with open(input_file_path, 'r') as file:
lines = file.readlines()
with open(output_file_path, 'w') as file:
for line in lines:
# 使用split('@')分割字符串,取第一部分(包名和版本号),然后去除可能存在的空格
cleaned_line = line.split('@')[0].strip()
file.write(cleaned_line + '\n')
print(f'Cleaned requirements have been written to {output_file_path}')
代码效果:
alabaster
alembic==1.13.1
anaconda-client==1.7.2
anaconda-navigator==2.0.3
anaconda-project
anyio
appdirs==1.4.4
argh==0.26.2
argon2-cffi
asn1crypto
astroid
astropy
async-generator
atomicwrites==1.4.0
attrs
autopep8
Babel
backcall
backports.functools-lru-cache
backports.shutil-get-terminal-size
backports.tempfile
backports.weakref==1.0.post1
bcrypt
beautifulsoup4
bitarray
bkcharts==0.2