ModuleNotFoundError when running flask

I’m new to coding, python and linux and am having an issue getting igraph to work properly.

I’m trying to develop a web app that utilizes igraph in the backend and am using an Ubuntu command line environment (vs code using Ubuntu 22.04 through WSL).

I am getting this error when trying to run a Flask application:

For some reason the import igraph command isn’t working despite vs code not indicating a problem ahead of runtime.

I originally installed igraph using pip3 install python-igraph.

I’m unsure if this will make sense but prior to using a flask implementation I had been experimenting with igraph in a graph.py file run in the same folder and it was executing without issues. However, I had to use some other version of python in vs code other than the recommended version. Again not sure I can explain it better now and I can’t find the settings.

The best way to debug this would be to check what the import path is in your code right before running import igraph, i.e. do this:

import sys
print(sys.path)
import igraph

Then check the entries in the import path one by one to see if the folder containing igraph/ is in the path or not.

Note that if you use the Flask dev server, it forks your process at startup; the main process becomes responsible for detecting changes in the code of your webapp, while the child process serves the incoming requests. The main process restarts the child process every time you change your code. There is a slight chance that sys.path in your child process (that eventually runs import igraph) is somehow different from sys.path in your main process or in the VS Code environment and that’s why you are getting this import error. Either way, you need to check what’s on sys.path.

I get the output below but don’t see igraph anywhere. NOTE this is run before calling a flask object so I believe I would not yet be dealing with any forking behavior mentioned in the reply.

How do I tell which path igraph is installed under (this is where my linux/ubuntu22.04 knowledge is very limited)? Do I not see it here because the print(sys.path) was run before the import igraph?

Debug Output:
/usr/bin/env
/home/#####/miniconda3/bin/python
/home/#####/.vscode-server/extensions/ms-python.python-2022.6.3/pythonFiles/lib/python/debugpy/launcher 42839 – /mnt/s/code/proj/cs50/final/proj/app.py
[
‘/mnt/s/code/proj/cs50/final/proj’,
‘/home/####/miniconda3/lib/python39.zip’,
‘/home/####/miniconda3/lib/python3.9’,
‘/home/####/miniconda3/lib/python3.9/lib-dynload’,
‘/home/####/.local/lib/python3.9/site-packages’,
‘/home/####/miniconda3/lib/python3.9/site-packages’]

Open a Python instance where you know that import igraph works, then type this:

import igraph
print(igraph.__file__)

This tells you the full path of the igraph module so you can see where it is imported from. It seems from your sys.path that there are multiple Python environments at play on your computer and I’m pretty sure that you are simply trying to import igraph from another one. For instance, in your last message, it seems like you are in a Python 3.9 environment, while in the first screenshot that you posted, it reports Python 3.10; those are definitely different environments.

I went back to my “non-flask” igraph file (graph.py) and confirmed it still worked. When input the

command it returns: /home/#####/miniconda3/lib/python3.9/site-packages/igraph/init.py

I believe this confirms that the igraph library is appropriately installed under python3.9/site-packages. I also see where my original error message is referencing python3.10. I have no idea why I would be using multiple environments. Wouldn’t the PIP install command automatically install under all python environments?

Also, I found where I changed the python environment I was utilizing. I am getting a strange issue with vs code complaining about problems in the code when I run the Recommended. It went away when I ran as Python 3.9.12(‘base’).

You can see here the problem lines when I am under 3.10.4 64-bit. I suspect this is related to some sort of bad installation of my libraries in the 3.10 environment.

image

Apparently you are using Python 3.10 and Python 3.9 in parallel; I can’t tell you why your computer is set up this way, but indeed you have multiple Python versions and in some cases you use one of them and in some other cases you use the other one.

No, because 1) pip wouldn’t know how many Python environments are on your computer (in my machine I have >50 of them, one for each project I am working on), and 2) Python packages that use C code (like igraph and many others) have to be compiled for a certain Python version, so a version that is compiled for Python 3.9 might not work with 3.10.

I can’t tell you from the screenshot what the problem is. Normally, VS Code has a “problems” panel at the bottom that lists the problems that it has found in your code.

Anyway, if you installed igraph in your Python 3.10 environment, you should not expect import igraph to work from your Python 3.9 environment, so either stick to 3.9 (but then you need to install igraph in the 3.9 environment) or to 3.10, don’t mix the two.

1 Like

Thanks I appreciate the help! Looks like I have some more investigating to do.

For clarity I did want to note the error I’m receiving in the Problems Panel:
Import “igraph” could not be resolved Pylance(reportMissingImports)

This only presents as a problem when I have Python 3.10.4 64-bit selected as an interpreter but does not have an issue when Python 3.9.12(‘base’: conda) is selected as an interpreter. Interestingly it also indicates a problem when Python 3.9.12(‘tf’: conda) is selected as an interpreter.

Either way, I see what you mean by multiple environments for different projects.

Solution: At some point I had installed miniconda3 when testing a different graphing solution. This installation was conflicting with both the Python version and also with Flask. Deleting miniconda3 and re running pip install igraph worked.