Some information about version i am using
pip -V
Result:
pip 6.1.1 from /usr/local/lib/python3.4/site-packages (python 3.4)
Next:
python -c "import sys,pip;print(sys.version,pip,pip.__version__)"
Result:
('2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]', <module 'pip' from '/usr/lib/python2.7/dist-packages/pip/__init__.pyc'>, '1.5.4')
I have installed pandas by
sudo pip install pandas
sudo easy_install pandas
I can see pandas in my system using
pip list
Result
certifi (14.5.14)
nltk (3.0.2)
numpy (1.9.2)
pandas (0.16.1)
pip (6.1.1)
pyparsing (2.0.3)
python-dateutil (2.4.2)
pytz (2015.2)
setuptools (2.1)
six (1.9.0)
tornado (4.1)
But when I import it, it throws error
python
>>> import pandas
Error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
Pandas is installed but why it is not working and how to make pandas work with my python interpreter
I don't see why you expect your Python 2 and Python 3 installations to be in sync. The output from pip -V clearly displays that it is running on Python 3.4, and the output of python -c "import sys,pip;print(sys.version,pip,pip.__version__)" clearly indicates it is using Python 2.7 (look at the version number in the library path).
By contrast, here's the same thing using a current virtual environment on my development system.
(dojo) airhead-2:tools sholden$ pip -V
pip 1.5.6 from /Users/sholden/dojo/lib/python3.4/site-packages (python 3.4)
(dojo) airhead-2:tools sholden$ python -c "import sys,pip;print(sys.version,pip,pip.__version__)"
3.4.2 (default, Oct 19 2014, 17:50:17)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] <module 'pip' from '/Users/sholden/dojo/lib/python3.4/site-packages/pip/__init__.py'> 1.5.6
You will see that the which command shows that pip and python both come from the same binary directory, and that the versions are matched.
(dojo) airhead-2:tools sholden$ which pip
/Users/sholden/dojo/bin/pip
(dojo) airhead-2:tools sholden$ which python
/Users/sholden/dojo/bin/python
Virtual environments were invented to keep the dependencies of one project from leaking into other projects. I suggest you think about starting to use them.
Related
I have already installed pytorch for MacOS 10.14 with pip3, but I can not import it in the python script. What should I do?
System: MacOS 10.14
Python3: v3.7
➜ ~ pip3 list
Package Version
----------- -----------
numpy 1.16.2
Pillow 5.4.1
pip 18.1
pycairo 1.17.1
pygobject 3.28.3
setuptools 40.5.0
six 1.12.0
torch 1.0.1.post2
torchvision 0.2.2.post3
virtualenv 16.1.0
wheel 0.32.2
➜ ~ python3
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'torch'
>>>
To expand upon my comment:
There's no strict guarantee that a pip3 wrapper script somewhere on your system is related to the pip package manager/module for your python3 binary. That wrapper may be created by a different installation of Python – maybe your system's own, maybe something else. (You can see where the script is located with which pip3 and see which interpreter it uses with less $(which pip3) and looking at the shebang line at the top.)
Each version of Python you have installed has its own site-packages directory, which contains the globally (as far as that version is concerned) installed packages. Fortunately, pip can be run exactly equally as the wrapper script would with the -m switch, so to be sure Torch and Torchvision get installed into your python3 (which appears to be Python 3.7.0 at this time),
python3 -m pip install torch torchvision
should do the trick.
However, globally (well, interpreter-globally, as discussed above) installed packages should be avoided, since you can easily get into hairy conflicts when you're working on multiple projects. You should instead use virtualenvs to separate your library installations from each other – the venv module is included in Python these days, and the official documentation has a guide on it. (Other options are pipenv and poetry, but it's worth knowing the lower-level tooling.)
After installing xlrd using pip, I am still unable to get it to work in my script. I'm not sure if I am missing something completely obvious - please help!
Currently, entering pip list on command line gives the following:
pip (9.0.1)
setuptools (32.1.0)
wheel (0.29.0)
xlrd (1.0.0)
And entering pip show xlrd gives:
Name: xlrd
Version: 1.0.0
Summary: Library for developers to extract data from Microsoft Excel (tm) spreadsheet files
Home-page: http://www.python-excel.org/
Author: John Machin
Author-email: sjmachin#lexicon.net
License: BSD
Location: /usr/local/lib/python2.7/site-packages
Requires:
Yet, if I go into the python environment (via typing python on command line) and try to import, here's what I get:
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlrd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named xlrd
Anyone has encountered this problem before? I also have python 3 installed, and I downloaded xlrd via pip3. Yet import xlrd doesn't work on either versions (2 or 3).
you can try this:
import pip
package_name='xlrd'
pip.main(['install', package_name])
First, you can check if such package actually exists on given directory (simply ls /usr/local/lib/python2.7/site-packages to see if there is __init__.py there).
If package is on place, you may check environmental variable $PYTHONPATH or just python -c 'import sys; print(sys.path)', which prints all directories where Python takes libraries from.
If everything is in place and sys.path contains site-packages (if that's not your first package installed via PIP I assume that everything's correct there), it might be problem with permissions. Please check some other stackoverflow question:
Cannot import a python module that is definitely installed (mechanize)
If package was installed with root's rw permission only - that would be the case.
Uninstall and install xlrd. May be at installing time you would have got some error. This error shows only if xlrd is not installed.
Use sudo pip install xlrd to install in python 2.7
I solved the issue with upgrading pip and following Bibek Ghimire's answer. It seems that I was using pip version 9 whereas version 18 has been already realized.
pip list
pip install xlrd
pip install xlrd == 1.2.0
now go to your IDE and start typing
import xlrd
its done now!
I installed pyenv to manage different versions of Python, and use pip install printtable to download and install printtable.
But when I import this module in interactive shell, it doesn't work and shows ImportError.
$ pyenv versions
system
2.7.11
* 3.5.1 (set by /Users/apple/.pyenv/version)
$ pip list
pip (8.0.0)
printtable (1.2)
setuptools (18.2)
$ python
Python 3.5.1 (default, Jan 21 2016, 12:50:43)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import printtable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/apple/.pyenv/versions/3.5.1/lib/python3.5/site-packages/printtable/__init__.py", line 1, in <module>
from printtable import PrintTable
ImportError: cannot import name 'PrintTable'
How can I manage the modules in pyenv?
PS. I'm following the book Automate the boring stuff step by step. The printtable part is in the end of Chapter 6.
Visit: https://automatetheboringstuff.com/chapter6/
I downloaded printtable with
pip3 install --download /tmp printtable
and inspected the contentents of printtable-1.2.tar.gz. In printtable/printtable.py there is code like
def printTable(self,line_num=0):
....
print self.StrTable
indicating that this package is not python 3 compatible.
It may be possible to install this module by
tar xfv printtable-1.2.tar.gz
cd printtable-1.2/
2to3-3.5 --write printtable/*.py tests/*.py
python3.5 setup.py build
python3.5 setup.py install
You’re not doing anything wrong; the printtable module is simply written in a way that’s entirely incompatible with Python 3.
Since you’re already using pyenv, and according to the output of pyenv versions you already have Python 2.7.11 installed, you can simply install printtable for that Python interpreter:
pyenv shell 2.7.11
pip install printtable
I'm running virtualenv on Windows 7 and I created a virtual environment for python 2.7 and installed some modules but I can't seem to import it.
1) venv\scripts\activate
2) pip2 install requests
(I the following message: get Requirement already satisfied (use --upgrade to upgrade): requests in c:\users\dan\desktop\python\bulkphotos\venv\lib\site-packages)
3) when I try to import this on the interactive shell, I get the following error
(venv) C:\Users\Dan\Desktop\Python\bulkphotos>py
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
4) I've tried searching SO but the other answers seem to be applicable for a mac (e.g. accidentally installing via sudo). When I do pip2 list, the module is present, so I don't understand why I can't access it.
(venv) C:\Users\Dan\Desktop\Python\bulkphotos>pip2 list
google-api-python-client (1.4.1)
httplib2 (0.9.1)
oauth2client (1.4.12)
pip (7.1.0)
pyasn1 (0.1.8)
pyasn1-modules (0.0.7)
requests (2.7.0)
rsa (3.2)
setuptools (18.0.1)
simplejson (3.8.0)
six (1.9.0)
uritemplate (0.6)
wheel (0.24.0)
(venv) C:\Users\Dan\Desktop\Python\bulkphotos>pip2 show requests
---
Metadata-Version: 2.0
Name: requests
Version: 2.7.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me#kennethreitz.com
License: Apache 2.0
Location: c:\users\dan\desktop\python\bulkphotos\venv\lib\site-packages
Requires:
(venv) C:\Users\Dan\Desktop\Python\bulkphotos>
Does anyone know how to solve this problem?
EDIT
The results of running sys.path on the interactive shell
['', 'C:\\windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages']
The results of running where py and where python
(venv) C:\Users\Dan\Desktop\Python\bulkphotos>where py
C:\Windows\py.exe
(venv) C:\Users\Dan\Desktop\Python\bulkphotos>where python
C:\Users\Dan\Desktop\Python\bulkphotos\venv\Scripts\python.exe
C:\Python27\python.exe
C:\Python34\python.exe
The Windows Python launcher PEP 397 is not aware of the active virtual env. See PEP 486 for details.
Try to launch Python interpreter by executing python instead of py.
I dont know why these packages are not imported,
But you can debug it by viewing your pythonpath using,
python -c "import sys; print sys.path"
You can append optional path to sys.path.
I would like to install a module but pip is not installing it in the right directory which I assume should be /usr/local/lib/python2.7/site-packages/. After all, I just installed Python 2.7.2 today. Originally I had 2.6.5 and had installed modules successfully there. So I think something is wrong with my Python path.
How to have all my module installations go to the proper python2.7 directory?
s3z#s3z-laptop:~$ pip install requests
Requirement already satisfied: requests in /usr/local/lib/python2.6/dist-packages/requests-0.6.1-py2.6.egg
Installing collected packages: requests
Successfully installed requests
s3z#s3z-laptop:~$ python
Python 2.7.2 (default, Oct 1 2011, 14:26:08)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
>>> ^Z
[3]+ Stopped python
Also here is what my Python directories look like now http://pastie.org/2623543
After you installed Python 2.7, did you install the Python 2.7 version of easy_install and PIP? The existing installations are configured to use Python 2.6 by default which may be causing your issue.
You are probably using pip linked to python2.6, instead of 2.7. If you have installed pip properly with python2.7, you can do:
pip-2.7 install requests
If not, try installing this way:
curl -O http://python-distribute.org/distribute_setup.py
[sudo] python2.7 distribute_setup.py
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
[sudo] python2.7 get-pip.py