离线环境下如何配置Cartopy全局共享的Natural Earth shapefiles路径?
Got it, let's fix this offline Cartopy shapefile problem once and for all. Since you're stuck in an offline environment and need a global shared location that every user can access (so Cartopy doesn't waste time trying to download files when it can't find them in the default ~/.local/share/cartopy/shapefiles/), here's exactly what you need to do:
First, choose a directory that all users on your system have read access to. Common, safe choices are:
/usr/share/cartopy/shapefiles//opt/cartopy_shapefiles/
- Create the directory (run as root/sudo):
sudo mkdir -p /usr/share/cartopy/shapefiles/ - Copy your pre-downloaded Natural Earth shapefiles into this directory, making sure to keep the subdirectory structure Cartopy expects. For example:
/usr/share/cartopy/shapefiles/ ├── natural_earth_lowres/ │ ├── ne_110m_admin_0_countries.shp │ ├── ne_110m_admin_0_countries.shx │ └── ne_110m_admin_0_countries.dbf └── natural_earth_10m/ └── [your 10m resolution shapefile files] - Set proper permissions so all users can read the files:
sudo chmod -R 755 /usr/share/cartopy/shapefiles/
You have two reliable methods—pick the one that fits your setup best:
Option 1: System-Wide Environment Variable (Recommended)
This method doesn't require modifying code and persists across Python/Cartopy updates.
- Edit a system-wide environment configuration file:
- For Debian/Ubuntu-based systems, edit
/etc/environmentand add this line at the end:CARTOPY_DATA_DIR="/usr/share/cartopy/shapefiles/" - For RHEL/CentOS-based systems, create
/etc/profile.d/cartopy.shand add:export CARTOPY_DATA_DIR="/usr/share/cartopy/shapefiles/"
- For Debian/Ubuntu-based systems, edit
- Make changes take effect: Either reboot the system, or have all users log out and log back in. Users can verify the variable is set by running:
echo $CARTOPY_DATA_DIR
Option 2: Modify Cartopy's Default Config File
Use this only if environment variables aren't feasible (note: this will be overwritten if you update Cartopy later):
- Locate Cartopy's
config.pyfile. It lives in your Python site-packages directory. For example:# For Python 3.10 on Ubuntu /usr/lib/python3.10/site-packages/cartopy/config.py - Open the file as root/sudo, find the
data_dirline. It will look like:data_dir = os.path.expanduser(os.environ.get('CARTOPY_DATA_DIR', '~/.local/share/cartopy')) - Replace the default path (
'~/.local/share/cartopy') with your global directory:data_dir = os.path.expanduser(os.environ.get('CARTOPY_DATA_DIR', '/usr/share/cartopy/shapefiles/')) - Save the file and exit.
Run a quick test script to confirm Cartopy uses the global files instead of trying to download:
import cartopy.crs as ccrs import matplotlib.pyplot as plt # Create a basic map with coastlines ax = plt.axes(projection=ccrs.PlateCarree()) ax.coastlines(resolution='110m') # This calls the lowres shapefiles plt.savefig('test_map.png') print("Test map created successfully—no download attempts!")
If this runs without errors and generates a map image, your setup is working correctly.
- Double-check subdirectory names in your global folder—Cartopy expects exact matches like
natural_earth_lowres,natural_earth_10m, etc. - If any user has a local
~/.local/share/cartopy/shapefiles/folder with incomplete/corrupted files, delete it so Cartopy falls back to the global path. - For the environment variable method, ensure the file you edit applies to all users (not just your personal shell config).
内容的提问来源于stack exchange,提问作者user3712093

