You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

离线环境下如何配置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:

Step 1: Pick & Prepare a Global Shared Directory

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/
  1. Create the directory (run as root/sudo):
    sudo mkdir -p /usr/share/cartopy/shapefiles/
    
  2. 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]
    
  3. Set proper permissions so all users can read the files:
    sudo chmod -R 755 /usr/share/cartopy/shapefiles/
    
Step 2: Configure Cartopy to Use the Global Path

You have two reliable methods—pick the one that fits your setup best:

This method doesn't require modifying code and persists across Python/Cartopy updates.

  1. Edit a system-wide environment configuration file:
    • For Debian/Ubuntu-based systems, edit /etc/environment and add this line at the end:
      CARTOPY_DATA_DIR="/usr/share/cartopy/shapefiles/"
      
    • For RHEL/CentOS-based systems, create /etc/profile.d/cartopy.sh and add:
      export CARTOPY_DATA_DIR="/usr/share/cartopy/shapefiles/"
      
  2. 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):

  1. Locate Cartopy's config.py file. 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
    
  2. Open the file as root/sudo, find the data_dir line. It will look like:
    data_dir = os.path.expanduser(os.environ.get('CARTOPY_DATA_DIR',
                                                '~/.local/share/cartopy'))
    
  3. 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/'))
    
  4. Save the file and exit.
Step 3: Verify the Setup Works

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.

Quick Tips to Avoid Headaches
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:54:09