Forums

pandas ImportError

Hi there, I've been trying to solve this issue all day now but haven't been able to do so. I want to host a small Flask based API to serve my machine learning model and access it from outside. When I run my WSGI file in the console, everything seems to work quite well (as it does on my local machine) but when I try to access it through the domain name username.pythonanywhere.com it throws an error.

This is the error log entry:

2023-09-28 10:51:48,308: Error running WSGI application
2023-09-28 10:51:48,319: ImportError: C extension: None not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext' to build the C extensions first.
2023-09-28 10:51:48,319:   File "/var/www/phylanx_pythonanywhere_com_wsgi.py", line 16, in <module>
2023-09-28 10:51:48,319:     from flask_app import app as application  # noqa
2023-09-28 10:51:48,319: 
2023-09-28 10:51:48,319:   File "/home/phylanx/mysite/flask_app.py", line 3, in <module>
2023-09-28 10:51:48,320:     import pandas as pd
2023-09-28 10:51:48,320: 
2023-09-28 10:51:48,320:   File "/home/phylanx/.local/lib/python3.10/site-packages/pandas/__init__.py", line 28, in <module>
2023-09-28 10:51:48,320:     raise ImportError(
2023-09-28 10:51:48,320: ***************************************************
2023-09-28 10:51:48,320: If you're seeing an import error and don't know why,
2023-09-28 10:51:48,320: we have a dedicated help page to help you debug: 
2023-09-28 10:51:48,320: https://help.pythonanywhere.com/pages/DebuggingImportError/

I'm out of options here, help would be highly appreciated.

Edit1: This is my requirements.txt

sentence-transformers==2.2.2
scikit-learn==1.2.2
Flask==2.2.2
joblib==1.2.0
pandas==2.1.0
pickleshare==0.7.5

I installed Pytorch manually via torch --extra-index-url https://download.pytorch.org/whl/cpu in order to get the much leaner cpu-only version.

Are you using a virtualenv?

Yes, I configured a vituralenv and linked it in the corresponding section on pythonanywhere

Is it ok if I have a look at your files?

sure! I actually found a hint just now. I'm using python 3.10 and it seems that the WSGI is run with python 3.8. This I deduced from the content of /var/lib/python " python3.8_installed".

That'll be the problem! You should update the web app python version so they match.

Alright, I'll do that and see what happens

Could you approve my edit of the original post? It has disappeared after my second edit and said it needed to be approved by an admin...

It's approved now.

I feel a bit stupid right now but I don't know how to do that. I want to create a virtual environment with python 3.8 and then run my flask app in there. I've set my python to 3.8 in the "Web" section but when I specify my environment further down, it says my virtual environment is 3.10.

Edit: I managed... Just needed to

run python3.8 -m venv

when creating the environment.

Yup, that would work! Personally I prefer to use virtualenvwrapper, which is a set of useful tools for managing virtualenvs, and (just for completeness) with that the command would be:

mkvirtualenv some-env-name --python=python3.8