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")
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")
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).
python -m venv ejemplo
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:
/usr/bin/python3
) 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:
/usr/bin/python3
) virtualenv --python=python3 ejemplo
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")