I installed the openpyxl module in a conda environment, and when launching a script which imports that module, Python throws a ModuleNotFoundError. I’d like to understand why. More details below.
I’m on Windows, and using miniconda3 to manage packages. I have only one environment named data_analysis, and in it I installed openpyxl using the conda install openpyxl
command from the Anaconda Prompt.
When checking to see if the package is in my environment using conda list openpyxl
, it shows that it is:
(data_analysis) C:\Users\tanqu>conda list openpyxl
# packages in environment at C:\Users\tanqu\miniconda3\envs\data_analysis:
#
# Name Version Build Channel
openpyxl 3.0.10 py312h2bbff1b_0
In the environment, if I open Python and import the openpyxl module, it works fine (as expected):
(data_analysis) C:\Users\tanqu>python
Python 3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, 17:20:38) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import openpyxl
>>>
However, when importing the module from a .py script wrote using Notepad++, I get a ModuleNotFoundError, and I don’t understand why. Below is the content of the .py script:
import tkinter.filedialog as TK
from pathlib import Path
import sys, os
import openpyxl
And the error I get:
(data_analysis) C:\Users\tanqu>C:\Users\tanqu\Desktop\Programming\scriptVerrier\scriptVerrier.py
Traceback (most recent call last):
File "C:\Users\tanqu\Desktop\Programming\scriptVerrier\scriptVerrier.py", line 6, in <module>
import openpyxl
ModuleNotFoundError: No module named 'openpyxl'
While searching for an answer, I found out that when launching my script from the Anaconda Prompt with the data_analysis environment activated, somehow it does not consider the current environment but instead goes back to the base environment.
Here’s what I get when adding print(sys.executable)
to the .py script:
(data_analysis) C:\Users\tanqu>C:\Users\tanqu\Desktop\Programming\scriptVerrier\scriptVerrier.py
C:\Users\tanqu\miniconda3
And here’s what I get when entering the same command while running the Python Interpreter from the Anaconda Prompt:
(data_analysis) C:\Users\tanqu>python
Python 3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, 17:20:38) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'C:\\Users\\tanqu\\miniconda3\\envs\\data_analysis\\python.exe'
Also, I have Python v3.11.5 in the base environment, and Python v3.12.0 in the data_analysis environment:
(data_analysis) C:\Users\tanqu>python
Python 3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, 17:20:38) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z
(data_analysis) C:\Users\tanqu>conda activate base
(base) C:\Users\tanqu>python
Python 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z
I don’t quite understand that behavior, any help would be greatly appreciated. I guess my question could be formulated like so: is there a way to specify which conda environment to consider from inside a .py script?
can you try running the script as
python C:\Users\tanqu\Desktop\Programming\scriptVerrier\scriptVerrier.py
. Make sure its from the terminal where you ahve activated the conda environment and “which python” outputs that environment python path.@HKay thanks a lot it worked, I do not encounter the ModuleNotFoundError when using your method. Do you know why?
I will add the details as an answer below.