Search code examples
pythonpython-3.x

ModuleNotFoundError in Python3.9


I am using python to do some data crunching on a computing node in my University. The computing node has multiple versions of python installed in it. However, every time I run a script using

$python3 script.py

by default python3.6 is chosen and I do not understand how to do I make python3.9 as the default version while running any script. Any guidance on this will be appreciated.

Further, I found a workaround on google and tried running my script with the command

$python3.9 script.py

This does choose python3.9 as the version to run, however it throws ModuleNotFoundError when I try to import any third party library say pandas. I have all the needed third party modules installed in the system and they work fine when I run a script with

$python3 scipt.py

I have to run the script using python3.9 because one of the modules fails if not run with python3.9. Do you have any suggestions on why none of the third party modules are not available when I switch to python3.9?


Solution

  • Consider using a python virtual environment it is highly recommended.

    minimal example:

    create venv

    python3.9 -m venv path/to/new/vnenv/folder
    

    activate venv (in linux)

    source path/to/new/vnenv/folder/bin/activate
    

    activate venv (in windows)

    path/to/new/vnenv/folder/Scripts/activate.bat
    

    install packages

    pip install pandas
    

    run script

    python script.py
    

    deactivate venv

    deactivate