Mismatch between pandas version in shell and conda - python

My Conda version is 4.11, and according to conda list my pandas version is 1.3.5. However, when I type python3 in the command line to bring up the shell and enter
import pandas as pd
pd.__version__
the version is 1.1.3.
The output of which -a python3 is
/home/a6623/anaconda3/bin/python3
/usr/bin/python3
as expected. Similarly, in the shell the output of sys.executable is
/home/a6623/anaconda3/bin/python3
as expected. If in the python shell I write pd.__file__ the output is
'/home/a6623/anaconda3/lib/python3.8/site-packages/pandas/__init__.py'
What is causing the version discrepancy?
Edit: upon further investigation, this discrepancy is also happening to other packages (e.g., networkx)

Solved! Package installed by Conda, Python cannot find it
Doing conda init then restarting the shell worked.

Related

Jupyter notebook does not reflect library version change in virtualenvironment

I am new to jupyter notebooks and virtualenvironment. I think that I am incurring in a really trivial problem.
I am trying to import matplotlib in a Jupyter notebook and the import works fine. However, it uses the wrong version of the library (installed also systemwide) and I don't know how to force it to use the one in the virtualenvironment.
In my virtualenvironment I want to use a most recent version of matplotlib and therefore I did
pip install --upgrade matplotlib in my virtualenvironment. The upgrade worked fine.
Now if I do pip show matplotlib in my virtualenvironment I get:
Name: matplotlib
Version: 2.2.5
...
Instead, if I do the same command in my home, I get:
Name: matplotlib
Version: 1.5.1
However, if in my jupyter notebook I do
import matplotlib
print ("matplotlib version:",matplotlib.__version__)
I get:
('matplotlib version:', '1.5.1')
Could you please help me understanding what I do wrong?
I found a solution to my problem. First of all, I read this long post that I suggest to everyone using conda or pip.
Then I understood that the shell environment is determined when the Jupyter notebook is launched, while the Python executable is determined by the kernel, and the two do not necessarily match.
I understood this by putting at the beginning of my jupyter notebook the following:
paths = !type -a python
for path in set(paths):
path = path.split()[-1]
print(path)
!{path} -c "import sys; print(sys.path)"
print()
!type python
import sys
sys.executable
!pip show matplotlib
It showed me that I was still using an older version of the matplotlib library.
Therefore I added
!{sys.executable} -m pip install --upgrade matplotlib
Restarted my jupyter kernel and this solved my problem.
I hope this solution will help someone else in the same situation.

Two versions of Pandas causing problems

It appears that when I run >conda list, I have two versions of pandas installed.
pandas 0.23.4 py36h830ac7b_0
pandas 0.22.0 <pip>
I cannot run import pandas or import pandas as pd in my console (Anaconda - Spyder/Jupyter Notebook) to check the version, but I am getting errors thrown in a script related to pandas:
Traceback (most recent call last) ...
from pandas.errors import
AbstractMethodError
ImportError: cannot import name 'AbstractMethodError'
I was going to do >conda update pandas but it said that my numpy would be downgraded. That doesn't sound right! How do I fix this?
It will be hard for someone on SO to debug your exact issue: The fastest way to fix your particular problem is most likely a fresh install of Anaconda. Then to set up a conda environment in your fresh install.
See the following:
Conda Environments: Creating an Environment
Powershell users will need this fix
This will avoid any conflicts with other python versions or pip
This will also allow you to maintain different environments with different versions of numpy or pandas
See below for an example of how simple this is to switch between 2.7 and 3.6
[py27] PS C:\Users\me> python --version
Python 2.7.15 :: Anaconda, Inc.
[py27] PS C:\Users\me> deactivate
Deactivating environment "py27..."
PS C:\Users\me> activate deeplearning
Activating environment "deeplearning..."
[deeplearning] PS C:\Users\me> python --version
Python 3.6.5 :: Anaconda custom (64-bit)

Conda openpxyl install results in No module named 'openpyxl'

I installed several conda environments with different Python versions.
After activating my Python 3.5 environment I have installed openpxyl via the line given here: https://anaconda.org/anaconda/openpyxl
However, when I try to import I get
ImportError: No module named 'openpxyl'
When I type conda list I do see
openpyxl 2.4.7 py35_0 anaconda
in the list.
(code to import is just import openpyxl or from openpyxl import *)
Any suggestions? Thanks!
You can try deleting all openpyxl material then reinstall it using pip.
that's how i solved my problem at least.
Even I faced the Same issue. This might Help you. If we have given python path in the windows environment variables which is present before we installed anaconda. We can test it by using
python --version
It will give you the version if the path is already present as
Python 3.6.5
Please remove this path of python which is already mentioned.
Then give the path of the python which is is anaconda directory. Normally it will in the path
C:\Users\lenovo\Anaconda3\
Just add this path to environment variables and again check for the python version
that might give you the following result
Python 3.6.5 :: Anaconda, Inc.
then it is confirmed that you are using the python which is downloaded using anaconda package manager.
Now you use the conda modules anywhere.

Cannot Import Python Packages on Linux

I ran this in the terminal to install all the packages for a machine learning project.
sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
It says the packages are already their newest version. When I run import numpy from the interpreter prompt, it just runs without a flaw.
But when I say numpy.version it says "module 'scipy.version' from '/usr/lib/python2.7/dist-packages/scipy/version.pyc'" without showing the version name. Also, the package import fails when I try a script.
On further experimentation, I figured a script as simple as
# Check python
print('Hello world')
is giving this error from a .py file,
./Script.py: line 2: syntax error near unexpected token `'Hello world''
./Script.py: line 2: `print('Hello world')'
Note that the script and version.version commands run fine from the terminal interpreter prompt.
Is something wrong very deep down?
Python version 2.7.12.
In order to get to know the version in python,
import numpy
print numpy.__version__
numpy.version gives the path.
If you want to check the version of numpy, try this:
import numpy
numpy.version.version
As for the following question, please post more information.

Importing mpl_toolkits.basemap on Windows?

I have recently started using Python 3.5 and Anaconda on my Windows pc. I am trying to plot a map. However, When I am in my Jupyter notebook and i type the command
import mpl_toolkits.basemap
I get an error message saying 'no module name' 'mpl_toolkits.basemap'
However, I have the module downloaded and in the same C:\Users\Geena file as my .matplotlib, .ipython, .jupyter files, etc.
Anyone know how I can fix this?
I've had this issue with anaconda on my windows 7.
I found the way to fix it with python 3.5:
You need to run with administrator rights "Anaconda Prompt" and in "Anaconda Prompt" run following command:
conda install -c conda-forge basemap-data-hires=1.0.8.dev0
, it will show new packages that you need to install and will ask you to install it - say 'Yes'.
After that new packages will be installed and the issue "import mpl_toolkits.basemap" will be fixed.
Thank you.
Currently, basemap is not compatible with python 3 for windows users. So, if you try conda install basemap and you have python 3 installed in windows, you'll see a message pointing out that a conflict was found with python 3.
I solved this by installing a python 2.7 environment. Try this:
http://conda.pydata.org/docs/py2or3.html
Then you just activate the python 2 environment. For example: activate py27 (py27 is the identifier of my python 2.7 environment).
After that, you can run conda install basemap with no conflict.
I'm using python 3.6.4 on Windows 7 Family Premium (32bit).
Because I was a bit frustrated by the message "no module named 'mpl_toolkits.basemap'", I searched for and tried a dozen of solutions without success : various versions, building from source, problems with VS version, nmake, ... You all know what I mean ;-)
I finally found a quite simple solution that works perfectly well for me :-) Here it is !
from here I downloaded basemap‑1.1.0‑cp36‑cp36m‑win32.whl
I changed the current dir to my download dir
I installed the wheel with python -m pip basemap‑1.1.0‑cp36‑cp36m‑win32.whl
I did the same for matplotlib‑2.2.3‑cp36‑cp36m‑win32.whl
You DO read the versions correctly : matplotlib 2.2.3 and basemap 1.1.0
Everything works fine for me and I finally can plot OSM POI's on a map of Belgium, without any 'trickery' at import :
import requests # to fetch OSM data
import json # to get the response
from mpl_toolkits.basemap import Basemap # ... Belgium is there !
import numpy as np # for arrays
import matplotlib.pyplot as plt # to build the populated map
Big big thanks to Christoph Gohlke (Danke Dir Christoph !) who did all the wonderful job !
When you have Anaconda, you don't download modules anywhere. In your command prompt, you type
conda install basemap
and it is installed with all its dependencies.
Anaconda requires an unusual install command for basemap 1.0.7.
https://anaconda.org/anaconda/basemap
To install this package with conda run:
conda install -c https://conda.anaconda.org/anaconda basemap
I just had this issue as well. All you need to do is update matplotlib by doing the following:
pip install --upgrade matplotlib
mpl_toolkits is part of matplotlib and just needs to be updated.
If you're using anaconda, the easiest thing to do is described here: in the conda prompt (as admin), type conda install -c anaconda basemap.
For people of the future : "Basemap is deprecated in favor of the Cartopy project."
https://matplotlib.org/basemap/users/intro.html#cartopy-new-management-and-eol-announcement

Categories