添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I can't seem to use sudo pip install correctly so that it installs into the following directory:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/

so that I can then import the module using python

I've run

sudo pip install scikit-learn --upgrade

Result

Requirement already up-to-date: scikit-learn in /usr/local/lib/python2.7/site-packages
Cleaning up...

However, it's not in the correct directory

How do I get sudo pip install to install into correct directory?

In addition, I've tried

sudo pip install Scrappy

I get the following message

new-host-2:site-packages Chris$ sudo pip install Scrapy
Password:
Requirement already satisfied (use --upgrade to upgrade): Scrapy in /usr/local/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): Twisted>=10.0.0 in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): w3lib>=1.8.0 in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): queuelib in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): pyOpenSSL in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): cssselect>=0.9 in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5.2 in /usr/local/lib/python2.7/site-packages (from Scrapy)
Requirement already satisfied (use --upgrade to upgrade): zope.interface>=3.6.0 in /usr/local/lib/python2.7/site-packages (from Twisted>=10.0.0->Scrapy)
Requirement already satisfied (use --upgrade to upgrade): cryptography>=0.2.1 in /usr/local/lib/python2.7/site-packages (from pyOpenSSL->Scrapy)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/local/lib/python2.7/site-packages (from zope.interface>=3.6.0->Twisted>=10.0.0->Scrapy)
Requirement already satisfied (use --upgrade to upgrade): cffi>=0.8 in /usr/local/lib/python2.7/site-packages (from cryptography>=0.2.1->pyOpenSSL->Scrapy)
Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/local/lib/python2.7/site-packages (from cffi>=0.8->cryptography>=0.2.1->pyOpenSSL->Scrapy)

Both these instances demonstrate that it's been installed but not correctly. For example, when I run the following import in python:

import scrapy
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-51c73a18167b> in <module>()
----> 1 import scrapy
ImportError: No module named scrapy

I've tried the following:

sudo ln -s /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
                You need to run pip installed for the python installation you want. i.e.: you probably have more than 1 python installed... check if you are running the python installed as a Framework or other one, and use pip from the one you want, it should be in the "bin" directory of the installation you want.
– dfranca
                Sep 1, 2014 at 14:27
                "type python" gives you the location you are running python from.  Then type "ls -l <python with full path>" to check if it's a link to another location. You probably want to run the one in /Library/Frameworks/Python.framework/Versions/2.7/bin/python
– dfranca
                Sep 1, 2014 at 14:50
                Thanks - you are absolutely right as I get "/usr/local/bin/python" from "type python." How would I delete that one or at least just get sudo pip install to always go into "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/"? In other words, how do I run pip installed for the python installation I want?
– Chris
                Sep 1, 2014 at 14:54
                You can create a link to the python you want.  i.e: sudo ln -s /usr/bin/python /Library/Frameworks/Python.framework/Versions/2.7/bin/python  But, I would recommend you run pip from the Framework, it's probably in the path /Library/Frameworks/Python.framework/Versions/2.7/bin/pip  So type /Library/Frameworks/Python.framework/Versions/2.7/bin/pip install <the lib>
– dfranca
                Sep 1, 2014 at 15:04

From the comments to the original question, it seems that you have multiple versions of python installed and that pip just goes to the wrong version.

First, to know which version of python you're using, just type which python. You should either see:

which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python

if you're going to the right version of python, or:

which python
/usr/bin/python

If you're going to the 'wrong' version. To make pip go to the right version, you first have to change the path:

 export PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin/python:${PATH}

typing 'which python' would now get you to the right result. Next, install pip (if it's not already installed for this installation of python). Finally, use it. you should be fine now.

Thanks for your suggestion! I'm having an issue with your solution though. I've tried to type in: export PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin/python:{PATH}. This doesnt work. I've tried to type in export PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin/python:{/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/}, which doesnt work. I've also tried export PATH=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/ . – Chris Sep 25, 2014 at 16:58 Hi Chris. First - I had a type in my original response - I missed a '$' sign in export PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin/python:${PATH}. Second - when you say it doesn't work - what do you mean by that? Could you be a bit more specific? – Roy2012 Sep 26, 2014 at 20:56 Note that it's easier to just run "python -m pip", to be sure that you're using the correct version of Python with pip. – rdb May 17, 2015 at 11:22
  • update path with correct python
  • uninstall pip using python -m pip uninstall pip setuptools
  • restart windows didn't work until a restart
  • #!/usr/bin/python
    # EASY-INSTALL-ENTRY-SCRIPT: 'pip==1.5.6','console_scripts','pip'
    __requires__ = 'pip==1.5.6'
    import sys
    from pkg_resources import load_entry_point
    if __name__ == '__main__':
        sys.exit(
            load_entry_point('pip==1.5.6', 'console_scripts', 'pip')()
    

    So if you got the same in MacOS, pip would always use /usr/bin/python.

    But this is a default. You can still provide the version of python you want either by editing the file or by using python explicitly.

    If which python returns /usr/bin/python then something went wrong when you installed your own version. If it is /Library/Frameworks/Python.framework/Versions/2.7/bin/python, you can directly call:

    sudo python `which pip` install scikit-learn --upgrade
    

    However, chances are high that it won't work. The reason is that sudo is resetting all your environment variables. To make it work, the easiest would be to use:

    sudo -E pip install scikit-learn --upgrade
    
    sudo -E python `which pip` install scikit-learn --upgrade
    

    depending on your setup.

    2 - What you should do

    pip was not thought of as something that root should execute. The actual best way to use it is to install a local, non-root, python version. You just have to make sure that you use it by default by setting up the correct environment variables (such as PATH on Linux) and then install pip without sudo using that python version.

    An even better way would be to setup virtualenvs from your root install.

    This way, you can install/update whatever you want without root privileges and never bother again about why sudo pip is not working. You would also avoid to provide root privileges to whatever is on Pypi and that would warrant that you don't mix system libs with your own.

    which python returns for me: /opt/local/bin/python (installed using macports) and this is the version I want to use. which pip returns /usr/local/bin/pip, that is associated with the default python. How can I install virtualenv using the macports-python given that I should use pip as per the manual (docs.python-guide.org/en/latest/dev/virtualenvs)? – Dror Jan 24, 2015 at 22:12

    I totally agree with the guys, it's better to use virtualenv so you can set a custom environment for every project. It ideal for maintenance because it's like a different world for every project and every update of an application you make won't interfere with other projects.

    Here you can find a nutshell of virtualenv related to installation and first steps.

    Well, the post was from two years ago. This link is useful as well simononsoftware.com/virtualenv-tutorial – Emily Feb 6, 2016 at 12:14
  • download pip at https://pypi.python.org/pypi/pip (tar)
  • unzip tar file
  • cd to the file’s directory
  • sudo python2.7 setup.py install
  • You could just change the shebang line. I do this all the time on new systems.

    If you want pip to install to a current version of Python installed just update the shebang line to the correct version of pythons path.

    For example, to change pip (not pip3) to install to Python 3:

    #!/usr/bin/python
    
    #!/usr/bin/python3
    

    Any module you install using pip should install to Python not Python.

    Or you could just change the path.

    This one worked perfectly for me. There are multiple pythons and multiple python3. Even after installing a package via pip or pip3, it always showed the package missing. I changed the first line of pip command to the same python my script refers to and installed package again. All sorted now. Thank you so much. – urvish Apr 19, 2022 at 10:00

    I've tried this and it worked for me,

    curl -O  https://files.pythonhosted.org/packages/c0/4d/d2cd1171f93245131686b67d905f38cab53bf0edc3fd1a06b9c667c9d046/boto3-1.14.29.tar.gz
    tar -zxvf boto3-1.14.29.tar.gz
    cd boto3-1.14.29/
    

    Replace X with your required python interpreter, for mine it was python3

    sudo pythonX setup.py install
    

    In Windows it happened to me when I upgraded the pip version from 23.0.1 to 23.2.1. This was in Python 3.10.11.

    To fix it I just downgraded to pip back to 23.0.1

    python -m pip install pip==23.0.1

    python -V

    then install the correct pip. You might already have intall in that case try to use:

    pip-2.5 install ...

    pip-2.7 install ...

    or for those of you using macports make sure your version match using.

    port select --list pip

    then change to the same python version you are using.

    sudo port select --set pip pip27

    Hope this helps. It work on my end.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.