If you encounter an ImportError or ModuleNotFoundError when attempting to import a Python library or install a package using pip inside Excel, the cause is architectural: native Python in Excel executes in an isolated, secure Microsoft Azure container, not on your local operating system. You cannot run pip install, shell out to a command prompt, or download arbitrary third-party packages from PyPI into the workbook.
Excel provides a curated, pre-installed Anaconda distribution of Python packages. Any attempt to reference external packages outside this distribution will fail.
Why pip and custom imports fail in Python in Excel
Python in Excel runs inside an isolated, sandboxed cloud container managed by Microsoft and Anaconda. Because the environment has no local filesystem access, interactive terminal, or outbound internet connectivity to PyPI, commands like
pip installor!pipare unsupported. You can only import libraries included in Microsoft’s pre-approved Anaconda runtime distribution.
Why Custom Package Installation Is Blocked
When you enter =PY() into an Excel cell, your formulas are sent securely to an isolated container in the Microsoft Cloud. To protect enterprise data and prevent remote code execution, Microsoft restricts this container’s operating parameters.
Excel Desktop / Online
│
│ (Encrypted Payload: Python code + Cell data)
▼
Microsoft Cloud Sandboxed Container (Azure)
│
├─► Standard Anaconda Python Environment (Pre-installed libraries)
│
├─► [BLOCKED] No Local OS / Terminal Access
├─► [BLOCKED] Outbound Internet / PyPI Access (Cannot run pip install)
└─► [BLOCKED] Custom C-Extensions / Binary Compilations
Because of this architecture:
- No outbound internet access: The container cannot connect to public package repositories such as PyPI or custom internal company registries.
- Stateless environment: Each calculation session runs within an ephemeral instance. Even if a package were somehow written to disk during calculation, subsequent recalculations or sessions would wipe the file system clean.
- Enterprise security: IT administrators govern Python in Excel through tenant-level administrative controls. Unvetted third-party libraries pose supply-chain risks, so the environment relies strictly on Anaconda-audited packages.
If your organization has disabled Python entirely, see our guide on Fixing “Python is disabled” security policy blocks in corporate environments.
Verifying Which Packages Are Supported
Microsoft and Anaconda include standard data science, mathematics, and machine learning libraries by default. The core suite includes:
- Data Manipulation:
pandas,numpy - Visualization:
matplotlib,seaborn - Statistics & Modeling:
scipy,statsmodels,scikit-learn - Utilities:
math,random,datetime,re
To verify which libraries are pre-installed in your tenant’s active Python runtime, enter the following code into an empty Python cell:
import pkg_resources
installed_packages = sorted([f"{d.project_name}=={d.version}" for d in pkg_resources.working_set])
installed_packages
Return the output as an Excel Value to see a spilled list of every installed package and version currently available in your environment.
If you attempt to import an available library like Pandas or Seaborn and still receive an error, check Fixing “ModuleNotFoundError” when calling Pandas or Seaborn in a cell. to diagnose import statement typos or initialization delays.
Diagnostic Paths: What to Do When You Need Unsupported Packages
If your calculation depends on an unsupported library (such as a niche machine learning tool, web scraper, or specialized optimization solver), you must use an alternative architectural approach.
Need an Unsupported Python Package?
│
├─► Is the calculation simple pure Python logic?
│ └─► YES: Replicate logic using pure Python functions or LAMBDA in Excel
│
├─► Is it an ETL / Data Transformation task?
│ └─► YES: Use Power Query before sending data to =PY()
│
├─► Does it require web scraping or external API calls?
│ └─► YES: Fetch external data via Power Query or local script first
│
└─► Does it strictly require proprietary / unlisted C-libraries?
└─► YES: Run Python locally outside Excel (via xlwings or local batch script)
Approach 1: Replicate Simple Package Utilities in Pure Python
Many small utility packages simply wrap mathematical formulas or string manipulations that can be written in native Python using built-in modules (math, re, itertools).
For example, instead of importing an external string similarity package, you can calculate Levenshtein distance or token matching using standard Python dictionary and loop operations directly inside the cell.
Approach 2: Offload Data Extraction to Power Query
If you were trying to import libraries like requests, BeautifulSoup, or urllib to pull live web data, the cloud container will block those network requests.
Instead:
- Use Excel’s built-in Power Query engine (
Data > From Web) to pull the data directly into an Excel Table. - Reference that resulting Excel Table inside your
=PY()cell usingxl("Table_Name[#All]", headers=True).
For details on how external boundaries impact your models, see Handling “Data Privacy Levels” that block Python from accessing external data..
Approach 3: Run Unsupported Workflows Outside native =PY()
When a project requires specific deep learning libraries (like PyTorch or TensorFlow) or internal proprietary wheels:
- Run Python locally: Use tools like Jupyter Notebooks, VS Code, or Python scripts using libraries such as
openpyxlorxlwings. - Export the computed outputs: Save the output arrays or summary metrics directly into a CSV or modern
.xlsxworkbook that Excel can ingest.
For a broader overview of how native Python fits into modern Excel workbooks, review The Python in Excel Diagnostic Masterclass: Troubleshooting the Modern Analyst Bridge.
Common Pitfalls When Importing in Excel
Attempting Terminal Commands Inside Cells
Entering shell-style commands directly into a formula cell will raise an immediate SyntaxError:
# Incorrect: This triggers SyntaxError
!pip install xgboost
pip install xgboost
All code in a =PY() block must be valid, executable Python syntax.
Importing Inside Every Cell Separately
While Python cells share global namespace state across a worksheet (calculating from top-left to bottom-right), repeating heavy import statements across dozens of cells introduces unnecessary overhead. Place your core imports once in an initialization cell located in row 1, column A.
If the formula bar hangs or fails during execution, consult Troubleshooting #PYTHON! Errors: Fixing Initialization and Connectivity..