70

Using a Raspberry Pi using Debian 4.14.34-v7+, I am trying to get pipenv set up with Python 3.6.5 as the default version of Python. I first install Python 3.6 by compiling it on the Pi (hours...). After making a 'robot' directory, I then install pipenv with sudo pip3 install pipenv and pipenv install --three.

Then I start the shell and open up Python, getting Python 3.5.3…

pi@raspberrypi:~/robot $ pipenv shell
Spawning environment shell (/bin/bash). Use 'exit' to leave.
. /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0/bin/activate
pi@raspberrypi:~/robot $ . /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0/bin/activate
(robot-XZ3Md9g0) pi@raspberrypi:~/robot $ python
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
[GCC 6.3.0 20170124] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

I then change the virtualenv by pipenv --python 3.6, but after it correctly (apparently) installs, I immediately get the warning that pipenv still expects Python 3.5…

(robot-XZ3Md9g0) pi@raspberrypi:~/robot $ pipenv --python 3.6
Virtualenv already exists!
Remove existing virtualenv? [Y/n]: y
Removing existing virtualenv…
Creating a virtualenv for this project…
Using /usr/local/bin/python3.6m (3.6.5) to create virtualenv…
⠋Running virtualenv with interpreter /usr/local/bin/python3.6m
Using base prefix '/usr/local'
New python executable in /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0/bin/python3.6m
Also creating executable in /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /home/pi/.local/share/virtualenvs/robot-XZ3Md9g0
Warning: Your Pipfile requires python_version 3.5, but you are using 3.6.5 (/home/pi/.local/share/v/r/bin/python).
  $ pipenv check will surely fail.
(robot-XZ3Md9g0) pi@raspberrypi:~/robot $ python
Python 3.6.5 (default, May  3 2018, 11:25:17) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

How do I set up pipenv to look for Python 3.6, when I first create the virtualenv? I can manually go in and edit the Pipfile, but that seems to be defeating the purpose of having pipenv take care of things for me.

6 Answers 6

121

"Edit the Pipfile" is the right way to go if you want to change the Python version of an existing environment.

If you want to create a new environment using Python 3.6, you can run

pipenv install --python 3.6

rather than

pipenv install --three

and that should do the trick.

Just be certain to delete the old Pipfile(s) if you create the new environment or else the commands will fail.

2
  • 1
    And then simply ignore warnings Your Pipfile requires python_version 3.5, but you are using X (...) pipenv check will surely fail. Commented Jul 16, 2020 at 8:22
  • 1
    Don't get it, if we use "--python 3.6" we MUST have python 3.6 already installed locally... Can't we have for example python 3.11 locally with pipenv installing a python 3.6 inside the venv ? Commented Sep 22, 2023 at 10:04
22

When setting up your pipenv you can specify the version of Python by using: pipenv --python 3.6, to use Python3.6.

Editing the Pipfile also works, by changing:

[requires] python_version = "3.6.6"

to the specific version of Python you want.

If you did all of this and are still having trouble with accessing the correct version of Python from within pipenv shell then you might want to check your .bashrc file to be sure that you don't have an alias for a specific version of Python.

1
  • This requires editing the Pipfile, which may be versioned etc. python_version does not accept ranges or multiple versions (which could be specified in order of preference). Consider poetry instead of pipenv for better support for Python versions. Commented Jul 16, 2020 at 8:25
18

If you want to create a virtual environment for your project the answers above might not work for you. For instance, if you have multiple versions of python installed and you had to rename the executables like I had to in the below example, OR for the same project you might want to create virtual environments that use different python versions.

When creating virtual environments for your projects, you can more specific and specify the path of the python executable.

Remember to run the below command relative to the project folder.

pipenv install --python 'C:\Python373\python373.exe'
1
  • 2
    works on ubuntu as well: pipenv install --python '/usr/bin/python3.7' thanks
    – C.K.
    Commented Sep 12, 2019 at 7:12
11

For an existing environment I could not figure out how to change the version of Python. I tried changing the Pipfile and specifying the path to the alternate version of python.

Instead I removed the virtualenv first using pipenv --rm and then started over using pipenv --python 3.10

1
  • I just spent so long stuck in that thank you!! Commented Apr 22 at 18:07
3

I suggest you two approaches:

  1. pip install --python 3.7 <your desired version number to be replaced>

(This requires either asdf or pyenv installed. If you don't have them installed, read on!)

  1. The manual method.(Applicable for Linux)(For win refer @Ryner's answer!)
  • Download the python gzip from python.org and extract it to a folder, cd to it.

  • Hit the following

     ./configure
    
     make 
    
     sudo make install
    
  • Now you have python version locally installed, all you gotta do is step one.

pipenv install --python <version-u-installed>

Note: If for any case, if you want to uninstall,go to the directory where you executed make, and simply hit make uninstall

Hope that helps! Happy coding!

-1

The above solutions only worked to me when I uninstalled both mysql-connector and mysql-connector-python and then reinstalled the mysql-connector-python.

- pip uninstall mysql-connector
- pip uninstall mysql-connector-python
- pip install mysql-connector-python

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.