ImportError: No module named 'pandas' (inside virtualenv) - python

I created a virtual environment named quora for python.
I installed wheel and then pandas as instructed.
I cant get pandas to work for some reason.
Can someone help me.
I have tried all the other solutions available to similar questions on this website. Still no use.
(quora) (jessie)griffith#localhost:~/environments$ sudo pip install wheel
Requirement already satisfied: wheel in /usr/lib/python2.7/dist-packages
(quora) (jessie)griffith#localhost:~/environments$ sudo pip install pandas
Requirement already satisfied: pandas in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: pytz>=2011k in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: numpy>=1.7.0 in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: python-dateutil in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/dist-packages (from python-dateutil->pandas)
(quora) (jessie)griffith#localhost:~/environments$ python getdata.py
Traceback (most recent call last):
File "getdata.py", line 2, in <module>
import pandas as pd
ImportError: No module named 'pandas'

I had this problem in a virtualenv with pip3 and pandas, tried all these previous answers, none of which actually work. but you can use easy_install pandas. et voilà.

Don't use sudo in a virtualenv — sudo pip install installs packages into global site-packages, not in virtualenv.
Either install pandas in the virtual environment (pip install after activating venv) or enable access to the global packages (recreate venv with option --system-site-packages or use command toggleglobalsitepackages from virtualenvwrapper).

I had the same problem. I fixed it by deleting my virtualenv directory and creating a new environment.

Check "which python" you are running using that command. You may need to export PATH to the python env instead of your default python which might be /usr/lib/bin. It might be installed in your quora env but the python that is being picked up is different and that doesn't have pandas

go to pyvenv.cfg and change
include-system-site-packages = false
to
include-system-site-packages = true

I tried all the answers before in my archlinux machine but none worked. But this post from Nikolai Janakiev helped me find a good solution.
Solution
Don't forget to first activate the virtual env, mine is named .venv:
$ source .venv/bin/activate
(.venv) $ python3 -m pip install ipykernel
Pick any arbitrary name and replace with NEW_KERNEL, this shows up in your jupyter notebook with the same name:
(.venv) $ python3 -m ipykernel install --name=NEW_KERNEL
And you're done!
Creating Virtual Environments
If you're not already familiar with setting up a virtual environment, here is the official guide.
Let's suppose you want to create a virtual environment under the name .venv. I use the . prefix to hide it by default.
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python3 -m pip install pandas
And voila! You have access to the pandas package in .venv environment.
Using Virtual Environments in Jupyter Notebooks
If you're wondering how to activate the virtual environment in a jupyter-notebook, just follow the Solution section above, and then open up the notebook, and click on Kernel then Change Kernel to NEW_KERNEL or the name you picked in the Solution section.

Related

Why am I unable to install package into a venv while it works using conda in the same machine?

I have created a venv which I call PyRepo from a Miniconda installation locally. So far it has been working well as I was able to install third party packages from my venv using pip install <PackageName> no problems. However, I bumped into this one package blpapi that will install from conda (in the same machine):
(base) C:\Programs\Miniconda3_64>pip install blpapi
Looking in indexes: https://nexus-tp.xxx.net/repository/public-pypi/simple
Requirement already satisfied: blpapi in c:\programs\miniconda3_64\lib\site-packages (3.14.0)
but when I attempt to do the same from the PyRepo venv then it produces the following error:
(PyRepo) c:\Xxx\Dev\PyRepo>pip install blpapi
Looking in indexes: https://nexus-tp.xxx.net/repository/public-pypi/simple
ERROR: Could not find a version that satisfies the requirement blpapi (from versions: none)
ERROR: No matching distribution found for blpapi
I have tried many different ways but they all fail with the same error:
pip install --user blpapi
python3 -m pip install blpapi
python3 -m pip install --pre --upgrade blpapi=3.14.0
pip install --isolated blpapi
I'm quite puzzled as what goes with this blpapi package as all other packages install fine in my venv. Also note that the installation on the conda environment uses the same nexus repository and it works while it doesn't in the venv. How can I fix this? are there any workarounds? e.g. copying the installation from the local conda to my venv PyRepo? How to manually copy the installation from conda to my venv safely or trigger a transfer install?

Installing a pip package from within a Jupyter Notebook not working

When I run !pip install geocoder in Jupyter Notebook I get the same output as running pip install geocoder in the terminal but the geocoder package is not available when I try to import it.
I'm using Ubuntu 14.04, Anaconda 4.0.0 and pip 8.1.2
Installing geocoder:
!pip install geocoder
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting geocoder
Downloading geocoder-1.15.1-py2.py3-none-any.whl (195kB)
100% |████████████████████████████████| 204kB 3.2MB/s
Requirement already satisfied (use --upgrade to upgrade): requests in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): ratelim in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): click in /usr/local/lib/python2.7/dist-packages (from geocoder)
Requirement already satisfied (use --upgrade to upgrade): decorator in /usr/local/lib/python2.7/dist-packages/decorator-4.0.10-py2.7.egg (from ratelim->geocoder)
Installing collected packages: geocoder
Successfully installed geocoder-1.15.1
Then try to import it:
import geocoder
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-4-603a981d39f2> in <module>()
----> 1 import geocoder
ImportError: No module named geocoder
I also tried shutting down the notebook and restarting it without any luck.
Edit: I found that using the terminal installs the geocoder package in /home/ubuntu/.local/lib/python2.7/site-packages and using a notebook installs it in /usr/local/lib/python2.7/dist-packages which is not in the path. sys.path.append('/usr/local/lib/python2.7/dist-packages') solves the problem for the current session.
So how can I permanently modify the path or tell pip where to install geocoder?
In IPython (jupyter) 7.3 and later, there is a magic %pip and %conda command that will install into the current kernel (rather than into the instance of Python that launched the notebook).
%pip install geocoder
In earlier versions, you need to use sys to fix the problem like in the answer by FlyingZebra1
import sys
!{sys.executable} -m pip install geocoder
! pip install --user <package>
The ! tells the notebook to execute the cell as a shell command.
%pip install fedex #fedex = package name
in 2019.
In older versions of conda:
import sys
!{sys.executable} -m pip install fedex #fedex = package name
*note - you do need to import sys
In jupyter notebook under python 3.6, the following line works:
!source activate py36;pip install <...>
The problem is that pyarrow is saved by pip into dist-packages (in your case /usr/local/lib/python2.7/dist-packages). This path is skipped by Jupyter so pip won't help.
As a solution I suggest adding in the first block
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
or whatever is path or python version. In case of Python 3.5 this is
import sys
sys.path.append("/usr/local/lib/python3.5/dist-packages")
This worked for me in Jupyter nOtebook /Mac Platform /Python 3 :
import sys
!{sys.executable} -m pip install -r requirements.txt
Try using some shell magic: %%sh
%%sh pip install geocoder
let me know if it works, thanks
Alternative option :
you can also create a bash cell in jupyter using bash kernel and then pip install geocoder. That should work
I had the same problem.
I found these instructions that worked for me.
# Example of installing handcalcs directly from a notebook
!pip install --upgrade-strategy only-if-needed handcalcs
ref: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
Issues may arise when using pip and conda together. When combining
conda and pip, it is best to use an isolated conda environment. Only
after conda has been used to install as many packages as possible
should pip be used to install any remaining software. If modifications
are needed to the environment, it is best to create a new environment
rather than running conda after pip. When appropriate, conda and pip
requirements should be stored in text files.
We recommend that you:
Use pip only after conda
Install as many requirements as possible with conda then use pip.
Pip should be run with --upgrade-strategy only-if-needed (the default).
Do not use pip with the --user argument, avoid all users installs.
conda create -n py27 python=2.7 ipykernel
source activate py27
pip install geocoder
Using pip2 worked for me:
!pip2 install geocoder
...
import geocoder
g = geocoder.google('Mountain View, CA')
g.latlng
[37.3860517, -122.0838511]

Python import fails Openpyxl

I know many questions speak about this problem, but i tried a lot and didn't fin any solution to my "very classical" issue : Python import fails altough package installed:
MacBook-Pro-de-Stephanie:scripts user$ sudo -H pip install openpyxl
Collecting openpyxl
Requirement already satisfied (use --upgrade to upgrade): jdcal in /usr/local/lib/python2.7/site-packages (from openpyxl)
Requirement already satisfied (use --upgrade to upgrade): et-xmlfile in /usr/local/lib/python2.7/site-packages (from openpyxl)
Installing collected packages: openpyxl
Successfully installed openpyxl-2.3.5
Package installed right ?
MacBook-Pro-de-Stephanie:scripts user$ python interactionsXLSX2CSV.py
Traceback (most recent call last):
File "interactionsXLSX2CSV.py", line 5, in <module>
from openpyxl import load_workbook
ImportError: No module named openpyxl
Fails right ?
MacBook-Pro-de-Stephanie:scripts user$ echo $PYTHONPATH
/usr/local/lib/python2.7/:
Path seems ok, no ?
I set permissions with:
sudo chmod -R ugo+rX ./lib/python2.7/site-packages/
What can i try else ?
Thank you very much
Here was the solution for me (linked to Mac OS clearly) : Can't load Python modules installed via pip from site-packages directory
/usr/bin/python is the executable for the python that comes with OS
X. /usr/local/lib is a location for user-installed programs only,
possibly from Python.org or Homebrew. So you're mixing different
Python installs, and changing the python path is only a partial
workaround for different packages being installed for different
installations.
In order to make sure you use the pip associated with a particular
python, you can run python -m pip install <pkg>, or go look at what
the pip on your path is, or is symlinked to.
I am not getting that error. Just now i installed. All i am getting is
" /usr/local/lib/python2.7/dist-packages/openpyxl/xml/init.py:15: UserWarning: The installed version of lxml is too old to be used with openpyxl
warnings.warn("The installed version of lxml is too old to be used with openpyxl")"
Once try by using below command.
sudo apt-get install python-openpyxl

Python packages not installing in virtualenv using pip

I'm having trouble installing twisted
pip --version
pip 1.1 from
/home/chris/GL/GLBackend/glenv/lib/python2.7/site-packages/pip-1.1-py2.7.egg
(python 2.7)
Create a virtual environment
chris#chris-mint ~/GL/GLBackend $ sudo virtualenv -p python2.7 glenv
Running virtualenv with interpreter /usr/bin/python2.7 New python
executable in glenv/bin/python2.7 Also creating executable in
glenv/bin/python Installing
distribute.............................................................................................................................................................................................done.
Installing pip...............done.
Just in case, I'll enable all permissions
chris#chris-mint ~/GL/GLBackend $ sudo chmod -R 777 glenv
chris#chris-mint ~/GL/GLBackend $ source glenv/bin/activate
(glenv)chris#chris-mint ~/GL/GLBackend $ pip freeze
argparse==1.2.1 distribute==0.6.24 wsgiref==0.1.2
twisted is not listed here as installed
(glenv)chris#chris-mint ~/GL/GLBackend $ sudo pip install twisted
Requirement already satisfied (use --upgrade to upgrade): twisted in
/usr/local/lib/python2.7/dist-packages Requirement already satisfied
(use --upgrade to upgrade): zope.interface>=3.6.0 in
/usr/local/lib/python2.7/dist-packages (from twisted) Requirement
already satisfied (use --upgrade to upgrade): distribute in
/usr/local/lib/python2.7/dist-packages (from
zope.interface>=3.6.0->twisted) Cleaning up... (glenv)chris#chris-mint
~/GL/GLBackend $ pip uninstall twisted Cannot uninstall requirement
twisted, not installed Storing complete log in
/home/chris/.pip/pip.log
But when I install it it says that its already installed.
Force the install:
sudo pip install -I twisted
Downloading/unpacking twisted Downloading Twisted-12.3.0.tar.bz2
(2.6Mb): 2.6Mb downloaded Running setup.py egg_info for package
twisted
. . .
Successfully installed twisted zope.interface distribute Cleaning
up...
And yet it still isn't installed
(glenv)chris#chris-mint ~/GL/GLBackend $ pip freeze
argparse==1.2.1 distribute==0.6.24 wsgiref==0.1.2
**When I try running Python scripts which use twisted, I get an error saying that twisted is not installed. That is:
ImportError: No module named twisted.python**
The problem here is that you're using sudo when you shouldn't be. And that's causing pip to try to install into /usr/local/lib instead of ~/glenv/lib. (And, because you used sudo, it's successfully doing so, but that doesn't help you, because you're not allowing system site-packages in your venv.)
There are multiple reasons sudo pip could lead to this behavior, but the most likely is this: On most systems (including the various Mac and RHEL/CentOS boxes I have immediate access to), the sudoers file will reset your environment, then add back in a handful of whitelisted environment variables. This means that when you sudo pip, it will not see the environment variables that virtualenv sets up, so it will fall back to doing the default thing and install into your system Python, instead of your venv.
But really, it doesn't matter why this is happening. The answer is the same: just do pip install instead of sudo pip install.
Note that you also want to remove the sudo on the virtualenv call, as this will probably cause the venv to be set up incorrectly (which is why you need the sudo chmod, which wouldn't be necessary otherwise). The whole point of installing things under your user home directory is that you can do it with your normal user permissions.
As a side note, you also may want to upgrade to a newer virtualenv/pip, as 1.8 and 1.2 have some bug fixes and improvements. But I verified that I get exactly the same problem as you even with the latest (1.8.4 and 1.2.1) versions, so I don't think that's relevant here.
The sudo pip is causing the problem here. It will install the package in your system instead of the virtual environment you created. So when it says the requirement is already satisfied. Try to look add the directory it is pointing to. Which is in your case, while you were trying to install, was /usr/local/lib/python2.7/dist-packages
If it's inside something like /usr/local/lib/..., which does not points to your virtualenv folder then it is installed in your system. Otherwise, in correct scenario it will look something like this /usr/local/lib/..../<name of your virtualenv>/lib.
You can always use commands like which python & which pip to see if they both are using the placeholder of our virtual environment.

How to install virtualenv scripts to /usr/bin

I install virtualenv with command sudo /usr/bin/pip-2.6 install virtualenv
And it says
Requirement already satisfied (use --upgrade to upgrade):
virtualenv in /usr/local/lib/python2.6/dist-packages
Cleaning up...
Why pip from /usr/bin looks to /usr/local/lib?
I need to install virtualenv scripts directly to /usr/bin, so I write
sudo /usr/bin/pip-2.6 install --install-option="--install-scripts=/usr/bin" virtualenv
But again it responds with
Requirement already satisfied (use --upgrade to upgrade):
virtualenv in /usr/local/lib/python2.6/dist-packages
Cleaning up...
Adding --upgrade doesn't help.
How can I install virtualenv scripts to /usr/bin ?
For your current issue can you first uninstall virtualenv using pip, and then reinstall using --script-dir=DIR, -s DIR as an --install-option.
As for your issue running pip with extra arguments and install not seeming to do anything, that may be fixed on develop branch at https://github.com/pypa/pip if not please file a bug with us on the GitHub issue tracker for pip.
As to why it is behaving like this - the install is based on the python installation not on the location of pip. Pip uses setuptools/distribute under the hood and conforms to the configuration of the python it is running under.
You can see where the version of python you are using installs to by running it - in this case probably python2.6 and querying the sys module.
>>> import sys
>>> sys.prefix
>>> sys.exec_prefix
See also distutils.sysconfig
For more information:
Installing Python Modules
setuptools
distribute
Try:
sudo /usr/bin/pip-2.6 install --install-option="--prefix=/usr/bin" virtualenv
(source).

Categories