Python 3 - How do you tell pipenv to use python 3 and not python 2?

Brent Heigold picture Brent Heigold · May 17, 2019 · Viewed 14.2k times · Source

I am trying to use the requests module, here is how I installed it:

[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.

Here is what it looks like when I run my script:

[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'

As you can see, the problem is that when I install it, it is using python 2 instead of python 3 (Using /usr/bin/python2 (2.7.14))

How can I tell pipenv to use Python 3 instead of Python 2?

Or is there a specific python 3 way of installing requests?

Answer

Giorgos Myrianthous picture Giorgos Myrianthous · May 17, 2019

The easiest way is to run (assuming you want 3.6):

pipenv --python 3.6

Alternatively and as a more persistent solution, you can add the following lines into the pip file:

[requires]
python_version = "3.6"