python-3.xminiconda

Why can't I load a module even though it's installed in my conda env?


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?


Solution

  • When you run a python file directly by its name such as C:\Users\tanqu\Desktop\Programming\scriptVerrier\scriptVerrier.py, the behavior depends upon the environment variables set on your machine (Windows in your case).

    As your system's PATH variable will have the default Python interpreter associated with .py extension that is why when you run your script directly by its name, it executes the file using that interpreter.

    To associate the python interpreter from the conda environment, you can change the environment variable path (not recommended) or you can simply associate the python interpreter on which you wants to execute your script.

    In your terminal when you add python before your file such as python C:\Users\tanqu\Desktop\Programming\scriptVerrier\scriptVerrier.py, it will execute the interpreter currently in use.

    You can check the interpreter details with which python command in the terminal.