我正在尝试使用Python 3在pipenv中安装库请求。我尝试过:
pip install requests
,在忘记pip
仅安装Python 2之前,导致,pip3 install requests
,肯定地返回Requirement already satisfied: requests in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (2.19.1)
Requirement already satisfied: idna<2.8,>=2.5 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (2.7)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (1.23)
Requirement already satisfied: certifi>=2017.4.17 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (2018.8.24)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from requests) (3.0.4)
当我用import requests
运行我的python文件时,一切正常,但是当我运行pipenv run python mypythonscript.py
时,我得到错误ModuleNotFoundError: No module named 'requests'
。请告知我缺少的东西。
这里的问题是pip3 install requests
在pipenv的virtualenv之外运行,它安装在virtualenv之外,因此在virtualenv中运行的代码中找不到requests
库。
将库安装到virtualenv中的正确方法是pipenv install requests
,它还会将requests
添加到您的Pipfile中。如果要暂时安装它而不保存到Pipfile中,可以运行pipenv run pip install requests
。