Issue
I am working through the tutorial on this webpage here in a Jupyter notebook on a Mac: https://towardsdatascience.com/kriging-the-french-temperatures-f0389ca908dd. Near the end of the exercise, I need to install the cartopy package. However, I get the error:
ModuleNotFoundError: No module named 'cartopy'
I've tried to directly install it by writing:
!pip install cartopy
but the same error appears. Reading through a few pages on Stackoverflow and Github suggest there is a conflict with the virtual environments and that this can not be installed with pip, but it has to be conda.
I'm fairly comfortable with Python, but the concept of virtual environments and pip vs. conda is completely foreign to me. Could someone help me solve this problem, but also explain why I'm unable to just pip install this package?
Thank you!
Solution
You need to use and understand virtual environments. pip and conda are both tools for this. Generally conda is preferred for cartopy as is will install non-Python tools like GDAL (cartopy installation docs). Essentially, an environment manager like conda helps you keep multiple environments on your computer with differing versions of packages.
Read about environment management with conda here
For your specific case, you need to:
- Download and install conda
- Make an environment file listing the packages you need. This will be a plaintext file called environment.yml
name: mapmaker # or whatever you want to to call it
channels:
- conda-forge # this is the repo that contains cartopy, among other tools
dependencies:
- ipython # for jupyter notebooks
- numpy
- pandas
- cartopy # you can keep adding more packages here
- Create an environment using this file (only need to do this once)
conda env create -f environment.yml
(See this SO answer)
- Activate the environment (you do this every time you need it)
conda activate mapmaker
conda is a versatile tool. I recommend reading their docs and searching for some tutorials on using conda for environment management
Answered By - marcos Answer Checked By - Candace Johnson (WPSolving Volunteer)