Installing a PyPI module by code

  • Hi!

    I'm trying to install a module from PyPI by code and it returns an error.

    I've tried this:

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

    and it fails with the output:

    Traceback (most recent call last):
    File "", line 2, in
    File "/Applications/", line 16, in main
    File "/Applications/", line 3, in
    File "/Applications/", line 10, in
    File "/Applications/", line 9, in
    File "/Applications/", line 7, in
    File "/Applications/", line 23, in
    File "/Applications/", line 12, in
    File "/Applications/", line 18, in
    File "/Applications/", line 33, in
    File "/Applications/", line 15, in
    File "distutils/command/install.pyc", line 18, in
    ImportError: cannot import name 'USER_BASE' from 'site' (/Applications/

    Also, I've tried this:

    import sys
    import subprocess
    output =[sys.executable, "-m", "pip", "install", "package_name"], capture_output=True)

    and got almost the same report with USER_BASE can't be imported, and also the same with pip show as subprocess.

    What I'm doing wrong?

Log in to reply