使用requirements.txt安装Python包

3
当我在创建虚拟环境后使用pip install -r requirements.txt安装包时,它们被安装了,但当我运行Python脚本时,仍然会出现ModuleNotFoundError错误。
我还尝试过pip install -r requirements.txt -t lib,但我仍然收到错误信息。
是否需要更多的步骤来安装这些包。非常感谢您的帮助。提前致谢。
更新:

requirements.txt

pandas
numpy
scikit-learn
flask
flask_restful
scipy
sqlalchemy

模块未找到错误:没有名为'flask'的模块

基本上,所有的模块都会给我一个错误。我必须逐个单独下载它们才能让脚本工作。


requirements.txt文件的内容是什么?你在哪个“模块”中遇到了错误? - undefined
嘿 @triandicAnt,我更新了问题。希望这有所帮助。 - undefined
你是否已经激活了你的虚拟环境? - undefined
对的,我有@shuttle87。 - undefined
1个回答

1

对我来说有效:

创建一个虚拟环境:

virtualenv test

激活你的虚拟环境:
source test/bin/activate

创建一个名为test的模块,并将requirements.txt文件放在其中。
cd test && sudo pip install -r requirements.txt

创建一个Python脚本test.py
import pandas as pd
import flask as fs

df = pd.DataFrame([[1,2]], columns = ['A', 'B'])
print(df)

运行脚本:
(test) test user$ python test.py

输出:

   A  B
0  1  2

如果我使用conda create -n test python=3.6创建虚拟环境,会有什么不同吗?因为这是我现在和之前几次创建虚拟环境的方式。我总是遇到这个问题。 - undefined
我没有使用过conda,但是如果你想在python3中创建一个虚拟环境,你可以使用virtualenv -p python3 虚拟环境名称 - undefined
1
你的例子起作用了。我会试着找出在我的情况下导致错误的原因。非常感谢。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接