4

On my MacBook Pro, I have Python 3.6 as default but the project I take over from another requires 2.7, which I have it installed via Anaconda. I set up the Pipenv using pipenv install which setup a version to 3.6. Then I tried to change the version by :

pipenv --python 2.7

but it returned this warning:

Warning: Your Pipfile requires python_version 3.6, but you are using 2.7.15 (/Users/j/.local/share/v/Z/bin/python).

Then of course pipenv check failed, and returned:

Specifier python_version does not match 3.6 (2.7).

Then I tried pipenv install python 2.7.15 and also failed. The Pipfile remains as 3.6 unchanged.

Error:  An error occurred while installing 2.7.15!
  Could not find a version that satisfies the requirement 2.7.15 (from versions: )
No matching distribution found for 2.7.15

Here is the python versions ls -ls /usr/bin/python*

32 -rwxr-xr-x  1 root  wheel  66880 24 Oct 12:47 /usr/bin/python
 0 -rwxr-xr-x  4 root  wheel    925 18 Aug 02:45 /usr/bin/python-config
 0 lrwxr-xr-x  1 root  wheel     75  8 Oct 21:45 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
 0 lrwxr-xr-x  1 root  wheel     82  8 Oct 21:45 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
32 -rwxr-xr-x  1 root  wheel  66880 24 Oct 12:47 /usr/bin/pythonw
 0 lrwxr-xr-x  1 root  wheel     76  8 Oct 21:45 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

Please advice how I can switch the python from 3.6 to 2.7 for this particular project using Pipenv?

All the best,

Jiajun

J_yang
  • 2,672
  • 8
  • 32
  • 61
  • Please read that solution. I believe you can find answer there. https://stackoverflow.com/questions/49794432/how-to-setup-a-pipenv-python-3-6-project-if-os-python-version-is-3-5 – Sergey Pugach Dec 07 '18 at 21:08
  • Well, it doesn't matter which is your system-wide python. I just cannot understand what's exactly your problem. If the python version in pipfile is 3.6 but you want 2.7, why cannot you just change 3.6 to 2.7 in pipfile? – Sraw Dec 07 '18 at 21:12
  • @Sraw, I tried directly changing the Pipfile to 2.7. But sys.version still return 3.6 – J_yang Dec 07 '18 at 21:31
  • You change that file and then change your pipenv's env to 2.7 too. So you can use 2.7 without failed check. As you got a failed check before, I believe you know how to change to 2.7. – Sraw Dec 07 '18 at 21:33

2 Answers2

7

The command you tried pipenv install python 2.7.15 is wrong for multiple reasons.

First of all, the format of the command pipenv is the following

pipenv install <package> <package>...

So when you run pipenv install python 2.7.15 you just tried to install two packages called respectively python and 2.7.15, which obviously is not what you wanted to do.

Even if you used the correct syntax pipenv install python==2.7.15 that would be wrong since you would be installing python 2.7.15 inside another python environment with python 3.6 installed (your system version on your laptop).

If you want to install multiple versions of Python in the same environment (read as your laptop) and don't messup with the system versions, you should use something like "pyenv" (https://github.com/pyenv/pyenv). Pyenv works very well with pipenv.

You will be able to install Python 2.7.15 with this command

pyenv install 2.7.15

As you can see this is different from the command you already tried pipenv install python 2.7.15.

Also since you have been having issues with your Pipfile, I would suggest to move that file together with Pipfile.lock in another directory (for backup purposes) and start from scratch with an empty directory.

Also as suggested here https://pipenv.readthedocs.io/en/latest/ it is better to create an empty folder ".venv" inside your root folder where all the python dependencies from that virtual environment will be installed.

So the correct list of commands to run is

pyenv install 2.7.15
mkdir .venv
pipenv --python 2.7.15
pipenv install <package>

I hope this solves your issues

PinoSan
  • 1,508
  • 16
  • 27
-1

If both python versions are in the path, then perhaps simply typing the following will work:

pipenv install --python 2.7

With the above command pipenv should search for python v2.7 and use it if it finds it. So if python 2.7 isn't in the path, you could try add it to the path.

Note:

I don't have a mac, but I have python for windows installed 4 times:

  • 3.7 (32 & 64 bit)
  • 3.7 (32 & 64 bit)

Only py is available in the path, which is a middle man for accessing the various python versions. When using pipenv, I use pipenv --python 3.7 and it correctly runs python (presumably using something like py -3.7-32, which is the only way on my computer to manually access a specific version, in this case python 3.7 32bit).

John
  • 59
  • 4