I'm trying to write a script that uses speedtest-cli, I have installed the module with pip install speedtest-cli. If I run pip install speedtest-cli I get the following
Requirement already satisfied: speedtest-cli in /usr/local/lib/python2.7/site-packages
If I try to use pip with the default python, as recommended by another answer here, like so python -m pip install speedtest-cli I get
/usr/bin/python: No module named pip
I figured that the pip is setup for one of the other versions of python but the only version of python in /usr/local/bin is python3 and in /usr/bin there is python, python2.6, and python2.7 but I have tried defining each ones of those as the interpreter at the top of the file and none of them works, each producing giving the same import error on the speedtest module.
Which version of python is pip linked to then, or why is the installed module not importing?
The easiest solution was to completed a fresh start.
Remove all references to easy_install, python, and pip using the following;
rm -r easy_install*
rm -r pip*
rm -r python*
Then I used this guide to properly install, setup, and configure python with HomeBrew, with pip and virtualenv installed.
The article is fairly lengthy so I'm not going to copy it all out here but here is an image snapshot in case the link goes dead.
Related
What is the difference between:
pip install --upgrade pip
and
python -m pip install --upgrade pip
and why is python -m pip install --upgrade pip generally favoured?
The difference is between pip and python -m pip; the rest of the command doesn't matter. The reason to prefer the latter is that you're ensuring that the python you normally use is the one which will provide the pip module you invoke. Otherwise, there is a risk that the pip executable found in your PATH is from an unrelated or out of date Python installation; it might install packages, but your regular python invocation won't find them (because they're installed for a non-default Python).
You can also modify the second command to invoke specific Python executable names (python2.7 vs. python3.8), or even absolute paths if you might have versions with the same name installed in multiple places.
The first one
pip install --upgrade pip
is to invoke pip as a command. The actual python interpretor called is not explicit. The second one is calling the python interpretor explicitly, so you know which one is called.
There should be no difference, as the __main__.py in the module and the pip script are both point to the same entry point, unless in the case that the default python is different from the one used by the pip script
If i am correct, pip install --upgrade pip and python -m pip install --upgrade pip are the same unless you specify the pip or python version.
The latter is preferred because it attempts to upgrade the pip associated with the specified python version (e.g. python3.7 -m pip install --upgrade pip) even if the main python version is different (python command may refer to any python version).
On Ubuntu 10.04 by default Python 2.6 is installed, then I have installed Python 2.7. How can I use pip install to install packages for Python 2.7.
For example:
pip install beautifulsoup4
by default installs BeautifulSoup for Python 2.6
When I do:
import bs4
in Python 2.6 it works, but in Python 2.7 it says:
No module named bs4
Alternatively, since pip itself is written in python, you can just call it with the python version you want to install the package for:
python2.7 -m pip install foo
Use a version of pip installed against the Python instance you want to install new packages to.
In many distributions, there may be separate python2.6-pip and python2.7-pip packages, invoked with binary names such as pip-2.6 and pip-2.7. If pip is not packaged in your distribution for the desired target, you might look for a setuptools or easyinstall package, or use virtualenv (which will always include pip in a generated environment).
pip's website includes installation instructions, if you can't find anything within your distribution.
You can execute pip module for a specific python version using the corresponding python:
Python 2.6:
python2.6 -m pip install beautifulsoup4
Python 2.7
python2.7 -m pip install beautifulsoup4
In Windows, you can execute the pip module by mentioning the python version ( You need to ensure that the launcher is on your path )
py -2 -m pip install pyfora
You can use this syntax
python_version -m pip install your_package
For example. If you're running python3.5, you named it as "python3", and want to install numpy package
python3 -m pip install numpy
Have tried this on a Windows machine and it works
If you wanna install opencv for python version 3.7, heres how you do it!
py -3.7 -m pip install opencv-python
Alternatively, if you want to install specific version of the package with the specific version of python, this is the way
sudo python2.7 -m pip install pyudev=0.16
if the "=" doesnt work, use ==
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev=0.16
Invalid requirement: 'pyudev=0.16'
= is not a valid operator. Did you mean == ?
x#ubuntuserv:~$ sudo python2.7 -m pip install pyudev==0.16
works fine
If you have both 2.7 and 3.x versions of python installed, then just rename the python exe file of python 3.x version to something like - "python.exe" to "python3.exe". Now you can use pip for both versions individually. If you normally type "pip install " it will consider the 2.7 version by default. If you want to install it on the 3.x version you need to call the command as "python3 -m pip install ".
Python 2
sudo pip2 install johnbonjovi
Python 3
sudo pip3 install johnbonjovi
For Python 3
sudo apt-get install python3-pip
sudo pip3 install beautifulsoup4
For Python 2
sudo apt-get install python2-pip
sudo pip2 install beautifulsoup4
On Debian/Ubuntu, pip is the command to use when installing packages
for Python 2, while pip3 is the command to use when installing
packages for Python 3.
for python2 use:
py -2 -m pip install beautifulsoup4
I faced a similar problem with another package called Twisted. I wanted to install it for Python 2.7, but it only got installed for Python 2.6 (system's default version).
Making a simple change worked for me.
When adding Python 2.7's path to your $PATH variable, append it to the front like this: PATH=/usr/local/bin:$PATH, so that the system uses that version.
If you face more problems, you can follow this blog post which helped me - https://github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence-exactly-for-centos-machine-only
As with any other python script, you may specify the python installation you'd like to run it with. You may put this in your shell profile to save the alias. The $1 refers to the first argument you pass to the script.
# PYTHON3 PIP INSTALL V2
alias pip_install3="python3 -m $(which pip) install $1"
I'm using Ubuntu 22.04, which comes with python 3.10.4.
Some packages do not have recent pip packages, so I needed install from an older pip. This sequence worked for me.
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9
sudo apt install python3.9-distutils
python3.9 -m pip install onnxruntime-gpu
Folder location: /usr/local/lib/python3.8
Package: python3.8 -m pip install <package_name>
I had Python 2.7 installed via chocolatey on Windows and found pip2.7.exe in C:\tools\python2\Scripts.
Using this executable instead of the pip command installed the correct module for me (requests for Python 2.7).
I think the best practice here is not to use the system python or install any system python package (no apt install). That is just the way to trouble.
Instead, build the required Python version from source, get it installed in /usr/local/... . Then use pip to install packages for that. It is really not that hard to build Python from source on Ubuntu.
sudo apt install build-essential
download the source from https://www.python.org/downloads/source/
unpack the file downloaded: tar xf <filename>
cd <directory> - change into the directory created.
./configure
make
sudo make install
Then check /usr/local/bin for a pip script tied to that version. Use that to pip install whatever you need. Also find the particular executable for the python version in that directory. You might have to shuffle things a bit if you get lots of versions.
Again, do not mess with system python.
I'm trying to test if requests module has been well installed. But I'm getting the following error :
raceback (most recent call last):
File "/Users/macbookpro/Desktop/test.py", line 1, in <module>
import requests
ImportError: No module named requests
when trying to run the following test script:
import requests
print 'test'
But I have installed requests with pip, and pip list command gives the following result :
MBPdeMacBook2:~ macbookpro$ pip list
arrow (0.7.0)
beautifulsoup4 (4.4.1)
classifier (1.6.5)
coursera-dl (0.6.1)
Django (1.8.6)
html5lib (1.0b8)
keyring (9.0)
lxml (3.6.0)
Pillow (3.4.2)
pip (8.0.2)
pyasn1 (0.1.9)
requests (2.14.2)
setuptools (19.4)
six (1.10.0)
urllib3 (1.16)
vboxapi (1.0)
virtualenv (13.1.2)
wheel (0.26.0)
Why requests isn't being imported ?
EDIT :
MBPdeMacBook2:~ macbookpro$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
MBPdeMacBook2:~ macbookpro$ which pip
/usr/local/bin/pip
MBPdeMacBook2:~ macbookpro$ python --version
Python 2.7.11
MBPdeMacBook2:~ macbookpro$ pip --version
pip 8.0.2 from /usr/local/lib/python2.7/site-packages (python 2.7)
In general, you should get into the habit of working in a virtualenv. I find the documentation here to be helpful.
If you install all of your dependencies within the virtual environment, you'll be (mostly) sure that you are installing those deps. in the same environment that you're running the jobs in.
For your case, on the command line go to the directory where your code lives and run
pip install virtualenv
virtualenv my_project
source my_project/bin/activate
Now that the virtualenv is active you can
pip install requests
Only what is installed in the virtualenv will be available. This will keep your system clean. Each project should get its own virtualenv, meaning only the dependencies needed for each project will be available to them. This way you could, say, have version 1 of some dependency installed for one project and version 2 for another. They won't come into conflict.
After you have installed all the dependencies, run
pip freeze > requirements.txt
To get a list of all the dependencies for the project saved. Next time you need to install these, you simply run
pip install -r requirements.txt
Once you are done working in the virtualenv, run
deactivate
I am not 100% sure, but the paths from which python and which pip may indicate that you have two versions installed. The Python version being the old one that was shipped with OS X, and another version.
I would advice you to install Python27 (or even better Python3) from brew.
You can install brew with a single command, and another one for installing Python27/3. When this is done you set the PATH variable in your shell rc file and you should be good to go.
I have Python27 installed (via brew) and my (working environment) reports the following paths:
which python: /usr/local/bin/python
which pip: /usr/local/bin/pip
And
python --version: 2.7.15
pip --version: pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python2.7)
I experienced this same issue on Ubuntu 18.04 LTS, to check that, I first checked if requests library was installed on my system or not.
Run these commands on your terminal inside the virtual environment in which you working
$ python
Then the python command line opens, then run
>>> import requests
After this if you get an ImportError saying, No module named requests, then it means the dependency has not been installed properly. If there is no such error, then it means the dependency is installed successfully.
This can occur for example if pip is actually pip3 and python is actually python2.7.
In your case the which pip and which python eliminate this possibility but it just happened to me.
The solution was to do pip2 instead of pip; if the situation was reversed you can use pip3.
Simply go inside your virtual environment and run below commands:
1). pip install --user pipenv
2). pipenv install requests
after executing the above commands cd to your app folder inside the virtual environment and just run it. Hopefully now it will run.
Reference link: https://python-guide-pt-br.readthedocs.io/pt_BR/latest/dev/virtualenvs.html#make-sure-you-ve-got-python-pip
I am trying to run Google's deep dream. For some odd reason I keep getting
ImportError: No module named google.protobuf
after trying to import protobuf. I have installed protobuf using sudo install protobuf. I am running python 2.7 OSX Yosemite 10.10.3.
I think it may be a deployment location issue but i cant find anything on the web about it. Currently deploying to /usr/local/lib/python2.7/site-packages.
There is another possibility, if you are running a python 2.7.11 or other similar versions,
sudo pip install protobuf
is ok.
But if you are in a anaconda environment, you should use
conda install protobuf
Locating the google directory in the site-packages directory (for the proper latter directory, of course) and manually creating an (empty) __init__.py resolved this issue for me.
(Note that within this directory is the protobuf directory but my installation of Python 2.7 did not accept the new-style packages so the __init__.py was required, even if empty, to identify the folder as a package folder.)
...In case this helps anyone in the future.
In my case I
downloaded the source code, compiled and installed:
$ ./configure
$ make
$ make check
$ sudo make install`
for python I located its folder(python) under source code, and ran commands:
$ python setup.py build
$ python setup.py install'
Not sure if this could help you..
I got the same error message when I tried to use Tensor Flow. The solution was simply to uninstall Tensor Flow and protobuf:
$ sudo pip uninstall protobuf
$ sudo pip uninstall tensorflow
And reinstall it again: pip installation of Tensorflow. Currently, this is:
# Ubuntu/Linux 64-bit, CPU only:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled:
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Mac OS X, CPU only:
$ sudo easy_install --upgrade six
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0rc0-py2-none-any.whl
when I command pip install protobuf, I get the error:
Cannot uninstall 'six'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
If you have the same problem as me, you should do the following commands.
pip install --ignore-installed six
sudo pip install protobuf
According to your comments, you have multiply versions of python
what could happend is that you install the package with pip of anthor python
pip is actually link to script that donwload and install your package.
two possible solutions:
go to $(PYTHONPATH)/Scripts and run pip from that folder that way you insure
you use the correct pip
create alias to pip which points to $(PYTHONPATH)/Scripts/pip and then run pip install
how will you know it worked?
Simple if the new pip is used the package will be install successfully, otherwise the package is already installed
I installed the protobuf with this command:
conda install -c anaconda protobuf=2.6.1
(you should check the version of protobuf)
In my case, MacOS has the permission control.
sudo -H pip3 install protobuf
I had this problem to when I had a google.py file in my project files.
It is quite easy to reproduce.
main.py: import tensorflow as tf
google.py: print("Protobuf error due to google.py")
Not sure if this is a bug and where to report it.
I am trying to install latest version of six python package but I have following issues. Can't get rid of six 1.4.1 in mac OSX 10.10.2
sudo pip install six --upgrade
Requirement already up-to-date: six in /Library/Python/2.7/site-packages
Cleaning up...
pip search six
six - Python 2 and 3 compatibility utilities
INSTALLED: 1.9.0 (latest)
python -c "import six; print six.version"
1.4.1
which -a python
/usr/bin/python
which -a pip
/usr/local/bin/pip
What is wrong here? Can't upgrade six!
I resolved the problem by the following method.
Download the six-1.10.0.tar.gz package
Use this command to install it.
python setup.py install
This works because it installs the new version of six to /Library/Python/2.7/site-packages/ which is searched before /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/
Your pip binary belongs to /usr/local/bin/python, whereas python points to /usr/bin/python. As a consequence
pip install --upgrade six
will install to /usr/local/bin/python.
The command below will make sure that the right version of pip is used:
python -m pip install --upgrade six
For me, just using homebrew fixed everything.
brew install python
What worked for me was to use easy_install instead of pip.
easy_install -U six
Easy_install managed to upgrade the package even when pip failed.
Mac OS X's default python is installed as a framework.
Under the framework directory, there is an 'Extras' directory and six package is already placed there.
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py
According to the description (https://github.com/MacPython/wiki/wiki/Which-Python), /System/Library/Frameworks/Python.framework/Versions/2.7/Extras is listed before /Library/Python/2.7/site-packages in module search path.
This means all packages already exists in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras can't upgrade.
Maybe you should install python manually and not to use default python.
I came across this exact issue when using pip to install the openstack client. My fix was to use easy_install instead of pip, as it utilizes /Library/Python/2.7/site-packages/ for module installation instead of the /System/Library/Frameworks/Python.framework/Versions/2.7/Extras. If this workaround is not an option for you, then I can confirm that #Masakazu Matsushita has the correct workaround of setting PYTHONPATH to /Library/Python/2.7/site-packages. To implement that workaround, add this line:
export PYTHON_PATH=/Library/Python/2.7/site-packages
to your ~/.bashrc and ~/.profile (if its a GUI Python application that you are trying to install).
Try these steps
Reinstall python using brew
$ brew install python
Resolve missing symlink problem
$ brew link --overwrite python
Reboot system or run
$ hash -r python
Try with pip2 its work for me
pip2 install -U six
While one or another of the above solutions may work for you, I think it's important to understand what is going on and what are the options that you have. I found this (rather lengthy) description to be very useful: it starts with outlining the options and only then suggests solutions.
In the end, the problem for me was that I was using the IPython shell.
which ipython returned /usr/local/bin/ipython and upon inspection this file declared at the top #!/usr/bin/python, which seemed to be circumventing all my best efforts to use the correct python location.
Simply changing this line #!/usr/local/bin/python to point to the correct python version then meant IPython used the correct six module.