Basemap with Python 3.5 Anaconda on Windows - python

I use Python 3.5 with latest version of Anaconda on Windows (64 bit). I wanted to install Basemap using conda install basemap. Apparently there is a conflict between Python 3 and basemap. After some googling indeed I found that basemap is not supported on Python 3 for Windows users (ex: https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/TjAwi3ilQaU).
For obvious reasons I do not want to downgrade to Python 2. What would then be the simplest alternative solution?
Is there an alternative package similar to basemap for ploting maps, etc.?
Should I use a second environment which uses Python 2 and basemap? I have never done that but it seems possible (http://conda.pydata.org/docs/py2or3.html). Is it "safe"? Should I install again all the other packages (matplotlib, numpy, etc.) on the second environment?
Thanks in advance for the help and advice.

Referring to the answer of Solly, I have Windows 10, python 3.5.3, Anaconda 64bit, in the Anaconda prompt I entered:
conda install -c conda-forge basemap=1.0.8.dev0
conda install -c conda-forge basemap-data-hires
then the code, taken from Python for Data Science for Dummies, page 193 (Plotting geographical data worked just fine.
I wanted to add just a comment to the Solly's answer, but I don't have enough credits to do so.
The code is:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
austin = (-97.75, 30.25)
hawaii = (-157.8, 21.3)
washington = (-77.01, 38.90)
chicago = (-87.68, 41.83)
losangeles = (-118.25, 34.05)
m = Basemap(projection = 'merc', llcrnrlat=10, urcrnrlat=50,
llcrnrlon=-160, urcrnrlon=-60)
m.drawcoastlines()
m.fillcontinents (color='lightgray', lake_color='lightblue')
m.drawparallels(np.arange(-90.,91.,30.))
m.drawmeridians(np.arange(-180.,181.,60.))
m.drawmapboundary(fill_color='aqua')
m.drawcounties()
x, y = m(*zip(*[hawaii, austin, washington, chicago, losangeles]))
m.plot(x,y, marker ='o', markersize=6, markerfacecolor='red', linewidth=0)
plt.title('Mercator Projection')
plt.show()

I have solved this several times (last time just now) by downloading it from
http://www.lfd.uci.edu/~gohlke/pythonlibs
and follow the instructions to install. From the anaconda command prompt
pip install full_path_to_package
For example, if you downloaded basemap-1.1.0-cp36-cp36m-win_amd64.whl, you would run
pip install C:\path\to\file\basemap-1.1.0-cp36-cp36m-win_amd64.whl
Note that the python version of the .whl file must match your python version. For example, ...-cp36-.... indicates Python 3.6. You can find your python version by running the command python --version.

I was running in the same problem (Python 3.5 and Anaconda) and eventually downloaded Basemap 1.0.8dev0 from here and installed it using conda (as described by the link).

Cartopy is an alternative to Basemap, and it is being actively developed.
There is a nice gallery here:
http://scitools.org.uk/cartopy/docs/latest/gallery.html

The below information is for Mac OS:
Downloaded from here!
Run conda install -c conda-forge basemap-1.2.0-py37h9622e30_3.tar.bz2
Done

Truth be told I had the same problem and tried to fix it for waaay to long and even tried a python 2 environment with no luck.
Personally just using a python 2 install was way easier and less time consuming. Sorry for the non answer.

Related

Module not found in Spyder outside of Anaconda

For a training course, I had to install Anaconda and launch Spyder from there. This used to work. However, I want to understand more about Python, because I have little experience with programming environments (I only use R and Stata up to now).
I stumbled on this issue: when I open Spyder directly (presumably I have downloaded a standalone version), most syntax runs well, but I cannot get the 'seaborn' module to work.
I tried pip install seaborn, pip3 install seaborn, conda install seaborn, but to no avail (outside of Anaconda, that is). I also tried updating spyder, homebrew, python and anaconda to the most recent versions. Probably this caused further issues discussed below.
My Python version is 3.9.7 (in terminal), Python3 is 3.9.9 (in terminal and through Anaconda), and through Spyder directly it is 3.9.5. Spyder IDE is 5.2.1, under Anaconda it is 5.1.5. I have no idea why these are all different, where to find the right paths, and how to update the Spyder version of Python and how to add the 'seaborn' module to that environment. It is possible to select a path for the Python interpreter in the Spyder preferences, but which path to take is unclear to me.
In short: how can I get seaborn to work in Spyder without having to rely on Anaconda (which seems a bit bloated to me and is slower to start up)?
Meanwhile, real problems occurred: I can't open Spyder from Anaconda any longer (so seaborn is not totally out of reach). This is the message:
/Users/myname/opt/anaconda3/bin/pythonw: line 3: /Users/myname/opt/anaconda3/python.app/Contents/MacOS/python: No such file or directory
This is the simple syntax I wanted to try:
# Seaborn example
import numpy as np
import pandas as pd
import seaborn as sns
sns.set_theme(style="whitegrid")
rs = np.random.RandomState(365)
values = rs.randn(365, 4).cumsum(axis=0)
dates = pd.date_range("1 1 2016", periods=365, freq="D")
data = pd.DataFrame(values, dates, columns=["A", "B", "C", "D"])
data = data.rolling(7).mean()
sns.lineplot(data=data, palette="tab10", linewidth=2.5)
Giving the error message: ModuleNotFoundError: No module named 'seaborn'
Probably this question is related: installed module in anaconda prompt shell but module not found in spyder?
I am working on an M1 MBA under Monterey.
Many thanks
I found the solution, which is to create a new environment and link to it, on the Spyder FAQ:
https://docs.spyder-ide.org/5/faq.html#using-packages-installer
The video was helpful: https://youtu.be/i7Njb3xO4Fw
It seems I can use the Python version installed through conda (in opt/anaconda3) and base an 'environment' on this, adding modules by first activating the environment and then using conda install.
Meanwhile, I still cannot launch Spyder from Anaconda-Navigator.
/Users/myname/opt/anaconda3/bin/pythonw: line 3:
/Users/myname/opt/anaconda3/python.app/Contents/MacOS/python:
No such file or directory
Probably reinstalling will solve this. But I'm good for now.

Different numpy version in Anaconda and numpy.__version__ in IPython Shell

I used How do I check which version of NumPy I'm using? to learn how to get the version of numpy. However, when I run conda list | grep numpy, I get:
numpy 1.15.2 py36ha559c80_0
numpy-base 1.15.2 py36h8128ebf_0
numpydoc 0.8.0 py36_0
However, when I run version from IPython shell, I get:
import numpy as np
np.__version__
Out: '1.13.3'
np.version.version
Out: '1.13.3'
np.version.full_version
Out: '1.13.3'
Why are the two versions different? Which one should I trust? Thanks for any help.
Please note that I am not using venv (i.e. virtual environment). I am directly accessing Anaconda's packages. So, there is no issue about versioning.
Here's what PyCharm is showing me:
As per Conda's version information on package doesn't correspond to __version__, here's __file__ and sys.path. Please note that I have hidden my name for privacy issues.
It seems that you have besides your python 3 environment in anaconda, another python with IPython and numpy installed.
It looks like that PyCharm and Anaconda see (correctly) the same numpy versions, while IPython which, I assume you didn't start from within your anaconda environment, sees another python installation with the older numpy. In fact, your output shows, that there is another python3.6 in C:\Users\... which doesn't belong to anaconda.
To make numpy 1.15 available in IPython you can either start IPython from within your anaconda environment by typing in the terminal (easier solution)
C:\>activate <your_anaconda_environment_name>
(<your_anaconda_environment_name>) C:\>ipython
or you make your local IPython load the modules from the anaconda environment by having a look at this answer. This will be not a recommended option in this case, given the resulting cross linkings of two python installations.
The issue is that PyCharm reads older python version from location App-data\roaming... What I did is that in start-up script, I added the following code.
print("Correcting sys paths now...")
paths = [
'C:\\Anaconda3\\python36.zip',
'C:\\Anaconda3\\DLLs',
'C:\\Anaconda3\\lib',
'C:\\Anaconda3',
'C:\\Anaconda3\\lib\\site-packages',
'C:\\Anaconda3\\lib\\site-packages\\win32',
'C:\\Anaconda3\\lib\\site-packages\\win32\\lib',
'C:\\Anaconda3\\lib\\site-packages\\Pythonwin',
'C:\\Anaconda3\\lib\\site-packages\\IPython\\extensions',
]
import sys
for path in reversed(paths):
sys.path.insert(0,path)
print("Completed correcting sys paths now...")
del path
del paths
Above code will force Python to read latest files from Anaconda. However, if you are using virtual environment, you would need to point to that environment.
If you want to know where is Python installed, you can run:
import os
import sys
os.path.dirname(sys.executable)
Above answer is inspired from conda python isn't using the numpy version I try install if I also specify that it should use python 2. It doesn't provide the solution. I have posted a solution above.

Solve atom error message

I received an error on atom, it asked to install ipkernal using pip.
Not sure what to do. I have Anaconda on my system and not pip. Can someone explains whats the error about and how can I solve it in using anaconda.
I was running a python code and saved the file as .py.
import pandas as pd
wd = pd.read_csv("winequality-red", sep = ";")
five = wd.head()
print ("five")
Error message:
No kernel for grammar Python found <br>
Check that the language for this file is set in Atom and that you have a Jupyter kernel installed for it.<br>
To detect your current Python install you will need to run:<br>
python -m pip install ipykernel<br>
python -m ipykernel install --user
This isn't really an answer, but you might have better luck on the dedicated Atom forums.
In your case though, it looks like you haven't installed the proper kernels Hydrogen needs to run Python with. (Of course, I'm just assuming you're using Hydrogen. You haven't actually provided any details about how you are trying to run it).
From the Hydrogen documentation, it takes you to this page for Python kernels.
https://nteract.io/kernels/python
In particular, I think you want to run the command conda install ipykernel

Python packages are imported on terminal but not on IDLE

So I am new in Python. I downloaded Anaconda (and Homebrew) and using Terminal on my Mac I confirmed that, say, Numpy is installed:
pip install numpy
to get as a result
Requirement already satisfied: numpy in ./anaconda/lib/python3.6/site-packages
Then opening Python within Terminal I can indeed import the package. The story is not the same once I open IDLE. So when I open it and try to import, say, Numpy I get the following message:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
I guess there is something I do not know, I assume it is a common rookie problem. Can you help me with this?
Since your IDLE is using Python 3 you should use
pip3 install numpy
to get it installed for the correct Python. If you want to use your default installed Python (2.x) instead, IDLE is located at /usr/bin/. From your terminal you can open it with
/usr/bin/idle
this should have the numpy you installed with pip install numpy
See when you are working with Anaconda it creates its own virtual environment.
Now, If you are new in python on MAC then you might not familiar with Virtual Environment.
I suggest you to download python from the official website www.python.org
or Click here to directly download Python 3.6.2 on your MAC.
Then, Download Pycharm(The Best IDE for Python)
Download the community version for beginner and it is also free.
Pycharm Community Version -> Click Here
And do whatever you want and also must read about Virtualenv
According to me, Pycharm is better then Anaconda's Jupyter Notebook
Wish you good luck and show your creativity in python ! !
Erm... well it appears as though - since you have 2 versions on your computer - that it might be because it imported it into the other file path directory. Now, I've never worked with Mac, but I think if you just specify which pip you want to download from, it might work.
For example, on Windows:
C:\Python34\Scripts\pip.exe install numpy
or if you were doing it for 3.6, you would follow the path to the folder, find pip and install. So, I suggest to install the normal IDLE before any other platform built upon it just because it is easier to import modules and is not as bad to break like yours has.
There can be two issues -
You are using python 2 as a kernel for IDLE, since numpy is installed for python3.6. This will raise an error.
The issue is with anaconda's configuration with IDLE. Anaconda installs numpy in ./anaconda/lib/python3.6/site-packages. If IDLE uses systems default python instead of anaconda's. You will not be able to import numpy.
You can try running this snippet -
import sys
print('\n'.join(sys.path))
to track the location of python exactly.
One of the workaround that I can think of is -
create a virtual environment using anaconda. Something like
conda create -n py352 python=3.5.2 anaconda
and then fire IDLE from your terminal. Though I am not entirely sure, if this works for mac. If it doesn't, let me know the output of -
import sys
print('\n'.join(sys.path))

MatPlotLib with Sublime Text 2 on OSX

I want to use matplotlib from my Sublime Text 2 directly via the build command.
Does anybody know how I accomplish that? I'm really confused about the whole multiple python installations/environments. Google didn't help.
My python is installed via homebrew and in my terminal (which uses brew python), I have no problem importing matplotlib from there. But Sublime Text shows me an import Error (No module named matplotlib.pyplot).
I have installed Matplotlib via EPD free. The main matplotlib .dmg installer refused to install it on my disk, because no system version 2.7 was found. I have given up to understand the whole thing. I just want it to work.
And, I have to say, for every bit of joy python brings with it, the whole thing with installations and versions and path, environments is a real hassle.
Beneath a help for this specific problem I would appreciate any helpful link to understand and this environment mess.
I had the same problem and the following fix worked for me:
1 - Open Sublime Text 2 -> Preferences -> Browse Packages
2 - Go to the Python folder, select file Python.sublime-build
3 - Replace the existing cmd line for this one:
"cmd": ["/Library/Frameworks/Python.framework/Versions/Current/bin/python", "$file"],
Then click CMD+B and your script with matplotlib stuff will work.
Well, this tutorial on how to install matplotlib in brew world helped me:
http://penandpants.com/2012/02/24/install-python/
While experienced problems installing scipy on Mountain Lion, this helped furthermore:
Scipy Installing to Wrong Place on Mountain Lion?

Categories