无网络环境下如何从Anaconda base环境复制指定包到新环境?
Hey there! This is such a common pain point when working offline with Conda—no need to waste time copying every single package from base. Let’s break down the best methods to transfer exactly the 10-20 packages you need, no internet required.
First, Understand Conda’s Local Cache
Conda automatically stores every package you’ve ever installed (including those in base) in a local cache directory. On most systems:
- Linux/macOS:
~/anaconda3/pkgs/ - Windows:
C:\Users\YourUsername\anaconda3\pkgs
All the .conda or .tar.bz2 files here are the installers for your base packages—we’ll leverage this instead of downloading anything new.
Method 1: Use Conda’s --offline Flag (Recommended for Conda-Installed Packages)
This is the simplest way if your target packages were installed via Conda in the base environment.
List base environment packages to get versions:
Run this to see all packages in base, note the exact version numbers for the ones you want:conda list -n baseFor example, you might see
numpy 1.26.0 py311h59b608b_0—you’ll neednumpy=1.26.0for the install command.Activate your new environment:
conda activate your-new-env-nameInstall specific packages offline:
Use the--offlineflag to force Conda to only use its local cache. List your target packages with their versions:conda install --offline numpy=1.26.0 pandas=2.1.3 matplotlib=3.8.0Conda will pull all these packages directly from the
pkgscache directory—no internet needed.
Method 2: Use a Package List File
If you have a long list of packages, it’s easier to create a text file with the package names and versions, then install from that.
Create a
required-packages.txtfile:
Add each package and its version on a new line, like this:numpy=1.26.0 pandas=2.1.3 scipy=1.11.3 seaborn=0.13.0Install from the file offline:
Activate your new environment, then run:conda install --offline --file required-packages.txt
Method 3: For Pip-Installed Packages in Base
If some of your target packages were installed via pip in base (not Conda), you can use Pip’s local cache to install them offline.
Locate Pip’s cache directory:
- Linux/macOS:
~/.cache/pip/wheels/ - Windows:
C:\Users\YourUsername\AppData\Local\pip\Cache\wheels
- Linux/macOS:
Install from local wheels:
Activate your new environment, then use--no-index(skip PyPI) and--find-links(point to local cache directories):pip install --no-index --find-links=~/anaconda3/pkgs --find-links=~/.cache/pip/wheels numpy pandasThis tells Pip to look for package wheels in both Conda’s cache and its own local cache.
Quick Note: There’s No --use-locals Flag, But...
You’re right that there’s no exact --use-locals parameter for Conda, but the --offline flag achieves exactly what you want—it restricts Conda to only use packages already present on your machine.
Troubleshooting Tip
If a package isn’t found in the cache, it’s likely that the base environment’s version was compiled from source (not downloaded as a pre-built package). In that case, you’d need to manually copy the package’s files from base/lib/pythonX.X/site-packages/ to your new environment’s site-packages/ directory, but this is rare for most common packages.
内容的提问来源于stack exchange,提问作者Mikhail_Sam

