Python 3——如何告诉pipenv使用Python 3而非Python 2?

16

我正在尝试使用requests模块,这是我安装它的方式:

[ec2-user@ip-xxx-xx-xx-xxx newslookup]$ pipenv install requests
Creating a virtualenv for this project...
Pipfile: /var/www/html/newslookup/Pipfile
Using /usr/bin/python2 (2.7.14) to create virtualenv...
⠇ Creating virtual environment...Already using interpreter /usr/bin/python2
  No LICENSE.txt / LICENSE found in source
New python executable in /home/ec2-user/.local/share/virtualenvs/newslookup-5acwuw4D/bin/python2
Also creating executable in /home/ec2-user/.local/share/virtualenvs/newslookup-5acwuw4D/bin/python
Installing setuptools, pip, wheel...
done.

✔ Successfully created virtual environment!
Virtualenv location: /home/ec2-user/.local/share/virtualenvs/newslookup-5acwuw4D
Creating a Pipfile for this project...
Installing requests...
Adding requests to Pipfile's [packages]...
✔ Installation Succeeded
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
✔ Success!
Updated Pipfile.lock (ab273c)!
Installing dependencies from Pipfile.lock (ab273c)...
     ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:01
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

这是我运行脚本时的样子:

[ec2-user@ip-xxx-xx-xx-xxx newslookup]$ python3 nasdaq_scrape_sec.py
Traceback (most recent call last):
  File "nasdaq_scrape_sec.py", line 5, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

问题在于我安装时使用的是Python 2而不是 Python 3,你可以看到这个信息:(Using /usr/bin/python2 (2.7.14))

如何告诉pipenv使用Python 3而不是Python 2?

或者是否有一种特定的Python 3方式来安装requests库?


1
输入 pipenv --python 3.5 命令,如需更多帮助,请输入 pipenv --help - Lokesh Sanapalli
当您启动Pipenv时,必须确保指定版本。不建议更改Pipfile并指定版本。 - mootmoot
从文档中得知:要初始化一个Python 3虚拟环境,请运行$ pipenv --three - VPfB
3个回答

24

最简单的方法是运行(假设您想要3.6):

pipenv --python 3.6

或者更持久的解决方案是,您可以将以下行添加到pip文件中:

[requires]
python_version = "3.6"

嗨Giorgos,这个pip文件在哪里?我正在使用Amazon Linux。 - Brent Heigold
通常情况下,PipfilePipfile.lock会在你运行pipenv的目录中创建。 - Giorgos Myrianthous
@BrentHeigold 请尝试在您的pipenv中运行pipenv install requests - Giorgos Myrianthous
我刚刚卸载了pipenv,并使用“pip3 install pipenv”重新安装了它,现在pipenv命令无法工作,这样做对吗? - Brent Heigold
现在它显示为“-bash:/usr/bin/pipenv:没有那个文件或目录”。 - Brent Heigold
显示剩余3条评论

7

创建环境时,Pipenv 会自动扫描您的系统,查找与给定版本匹配的 Python 版本。

pipenv --python VERSION

这不是最佳答案吗?! - Y2H

4

只需从Python3中调用pipenv,它就会完成工作。

 python3 -m pipenv install requests

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