Install pip with idle

1

I have been asking for a while if there is a possibility to install libraries from the same IDLE, that is:

import pip  
pip.install("asciimatics")
    
asked by Alex 16.07.2018 в 09:38
source

1 answer

0

It is possible, but it is not designed to be used in that way, so the interface (that is, the way to launch it from a script) can change according to what version of python and pip you have.

To begin with, pip is a Python module only from Python 3.4, before it was an executable to be installed separately. On the other hand, there are python distributions that do not include it. Finally, it seems that the pip module that you import when doing import pip is different depending on whether you are within a virtual environment or not.

Having said all this, I have succeeded in making the following work (but it may well stop working in another version).

  • Operational: OSX
  • Python 3.6.4
  • Virtual environment created with python -m venv ejemplo
  • and activated with source ejemplo/bin/activate

Example that installs the asciimatics package from a python script:

import pip
pip.main(['install', 'asciimatics'])

The same script works also in:

  • Operational: Linux Ubuntu 14.04
  • Python: 3.5.2 ( /usr/bin/python3 )
  • No virtual environment (global installation)

But the same script, even in the same operation and same version of python, no longer works if you are in a virtual environment created with virtualenv , that is:

  • Operating System: Linux Ubuntu 14.04
  • Python: 3.5.2 ( /usr/bin/python3 )
  • Virtual environment created with: virtualenv --python=python3 ejemplo
  • and activated with: source ejemplo/bin/activate

To work in this case you have to use this other:

import pip
from pip._internal import main as pip_main
pip_main(['install', 'asciimatics'])

In summary, I do not think it is a highly recommended route. The same is more portable a simple:

import os
os.system("pip install asciimatics")
    
answered by 16.07.2018 в 10:20