Why does “pip install” inside Python raise a SyntaxError?

后端 未结 8 844
天命终不由人
天命终不由人 2020-11-21 05:46

I\'m trying to use pip to install a package. I try to run pip install from the Python shell, but I get a SyntaxError. Why do I get this error? H

相关标签:
8条回答
  • 2020-11-21 06:20

    As @sinoroc suggested correct way of installing a package via pip is using separate process since pip may cause closing a thread or may require a restart of interpreter to load new installed package so this is the right way of using the API: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'SomeProject']) but since Python allows to access internal API and you know what you're using the API for you may want to use internal API anyway eg. if you're building own GUI package manager with alternative resourcess like https://www.lfd.uci.edu/~gohlke/pythonlibs/

    Following soulution is OUT OF DATE, instead of downvoting suggest updates. see https://github.com/pypa/pip/issues/7498 for reference.


    UPDATE: Since pip version 10.x there is no more get_installed_distributions() or main method under import pip instead use import pip._internal as pip.

    UPDATE ca. v.18 get_installed_distributions() has been removed. Instead you may use generator freeze like this:

    from pip._internal.operations.freeze import freeze
    
    print([package for package in freeze()])
    
    # eg output ['pip==19.0.3']
    


    If you want to use pip inside the Python interpreter, try this:

    import pip
    
    package_names=['selenium', 'requests'] #packages to install
    pip.main(['install'] + package_names + ['--upgrade']) 
    # --upgrade to install or update existing packages
    

    If you need to update every installed package, use following:

    import pip
    
    for i in pip.get_installed_distributions():
        pip.main(['install', i.key, '--upgrade'])
    

    If you want to stop installing other packages if any installation fails, use it in one single pip.main([]) call:

    import pip
    
    package_names = [i.key for i in pip.get_installed_distributions()]
    pip.main(['install'] + package_names + ['--upgrade'])
    

    Note: When you install from list in file with -r / --requirement parameter you do NOT need open() function.

    pip.main(['install', '-r', 'filename'])
    

    Warning: Some parameters as simple --help may cause python interpreter to stop.

    Curiosity: By using pip.exe you actually use python interpreter and pip module anyway. If you unpack pip.exe or pip3.exe regardless it's python 2.x or 3.x, inside is the SAME single file __main__.py:

    # -*- coding: utf-8 -*-
    import re
    import sys
    
    from pip import main
    
    if __name__ == '__main__':
        sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
        sys.exit(main())
    
    0 讨论(0)
  • 2020-11-21 06:21

    Initially I too faced this same problem, I installed python and when I run pip command it used to throw me an error like shown in pic below.

    Make Sure pip path is added in environmental variables. For me, the python and pip installation path is::
    Python: C:\Users\fhhz\AppData\Local\Programs\Python\Python38\
    pip: C:\Users\fhhz\AppData\Local\Programs\Python\Python38\Scripts
    Both these paths were added to path in environmental variables.

    Now Open a new cmd window and type pip, you should be seeing a screen as below.

    Now type pip install <<package-name>>. Here I'm installing package spyder so my command line statement will be as pip install spyder and here goes my running screen..

    and I hope we are done with this!!

    0 讨论(0)
提交回复
热议问题