2

I'm trying to deploy a Python Flask app (for API endpoint) on CentOS for work, but every online guide I've followed isn't working for me!

python3 -m venv APIenv

The env was created successfully, but then trying to activate it:

source APIenv/bin/activate

Returns nothing, neither an error not is it activating it!

from what I understood, activating it should make the CLI look like this:

(venv) $ _

I'm also having another problem:

pip3 install firebase_admin

this worked and it was installed, but then running my app:

python3 app.py

Traceback (most recent call last): File "app.py", line 4, in import firebase_admin ModuleNotFoundError: No module named 'firebase_admin'

Every online guide I've followed is getting me stuck here, I really need to get this to work, what am I doing wrong?

KiDo
  • 145
  • 9

1 Answers1

3

Probably you have pip3 linked to another version of python(3.6,3.8, etc) than python3 which you are using

Make sure you are using the same versions and it is in venv actually check python3

# python3 -V
Python 3.8.6
# which python3
/tmp/APIenv/bin/python3
# ls -laht /tmp/APIenv/bin/python3
lrwxrwxrwx 1 root root 8 Sep 30 13:59 /tmp/APIenv/bin/python3 -> python38

Check pip3

# pip3 -V
pip 19.3.1 from /tmp/APIenv/lib64/python3.8/site-packages/pip (python 3.8)
# which pip3
/tmp/APIenv/bin/pip3
# ls -laht /tmp/APIenv/bin/pip3
-rwxr-xr-x 1 root root 225 Sep 30 13:59 /tmp/APIenv/bin/pip3

sometimes it can be looking so even under VENV:

#  which python38
/tmp/APIenv/bin/python38
# ls -laht /tmp/APIenv/bin/python38
lrwxrwxrwx 1 root root 17 Sep 30 13:59 /tmp/APIenv/bin/python38 -> /usr/bin/python38

Check paths of your pip and python interpreter then, to be sure you can use it by absolute path of your venv

Updated:

Issue related to python3.6 version.

firebase_admin cannot be installed with python3.6 version

Required at least python3.8 (3.7 not tested)

Python38 installation for Centos 8 and related question modules:

dnf install python38 python38-pip
pip38 install firebase_admin flask venv
  • python3 -V => Python 3.6.8 | which python3 => /bin/python3 | As for pip: pip3 -V => pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6) | which pip3 => /bin/pip3 | Is the problem Python is 3.6.8 while pip is only 3.6? – KiDo Sep 30 '21 at 12:07
  • 1
    no, it is not an issue, is that output under VENV? as i see there is system environment paths, not from virtual environment, it looks as an issue – Aleksandr Chendev Sep 30 '21 at 12:20
  • I'm sorry, I didn't understand, if it helps, I'm using root, installed python & pip before moving into venv directory – KiDo Sep 30 '21 at 13:48
  • 1
    you should activate virtuan environment with command `source APIenv/bin/activate` then you will see such prefix in the shell `(venv) $ _`, then run commands `pip3 install firebase_admin` and `python3 app.py`, all of it in virtual environment which you can notice by prefix `(venv) $ _` – Aleksandr Chendev Sep 30 '21 at 14:23
  • I am trying that, but it returns nothing, as you mentioned, the shell should change to ```(venv) $``` but in my case it remain the same ```[root@ MyAPI]#``` – KiDo Oct 01 '21 at 06:20
  • 1
    so, you have 2 issue, one related to venv activation and sercond i'm pretty sure issue with `pip3 install firebase_admin` because of python3.6 version firebase_admin trying to install but failed at the end, you should install newer version of python, at least 3.8 then run something like `pip38 install firebase_admin flask venv` and trying steps from your question above again, but with using new version with `python38` instead python3 – Aleksandr Chendev Oct 01 '21 at 12:55
  • I followed those steps and everything seems to be working, the only problem left is that I still can't access it using public IP (other PCs), I'm running it with: ```flask run --host=0.0.0.0``` and I've opened port 5000, but trying to check it using Postman returns Connection Timed Out – KiDo Oct 05 '21 at 11:54
  • 1
    try clearing iptables rules if it possible with `iptables -F` and check connection, if it will work you can creating iptables accept rule for that port, if it doesn't help - provide me flask terminal output during start – Aleksandr Chendev Oct 05 '21 at 13:32
  • [root@ MyAPI]# flask run --host=0.0.0.0 * Serving Flask app 'app.py' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployme nt. Use a production WSGI server instead. ... – KiDo Oct 05 '21 at 13:41
  • * Debug mode: off * Running on all addresses. WARNING: This is a development server. Do not use it in a production deployme nt. * Running on http://serverIP:5000/ (Press CTRL+C to quit) – KiDo Oct 05 '21 at 13:41
  • 1
    what the `serverIP:5000` ?, there should be `Running on http://0.0.0.0:5000/` do you have app.run string like that `app.run(host='0.0.0.0', port=5000)` inside app or does it looks different? also you should have realy public ip address (which not related to private range) – Aleksandr Chendev Oct 05 '21 at 14:13
  • serverIP is my server’s IP: ‘’’xx.xx.xx.xx:5000’’’, and in app.run, I just have “app.run()”, because Flask guide says I just need to run this command to make it public: “flask run —host=0.0.0.0”, and I do have a public IP, it’s where my WordPress website is hosted on. – KiDo Oct 05 '21 at 15:23
  • 1
    if you say so and you have not able to connect with public ip and 5000 port it's network issue on server OS side (related to input connection rules) or at your side (router, internet provider, etc, something that can blocking output connects to 5000 port), you can provide output of `iptables-save` then i'd look if there some issue – Aleksandr Chendev Oct 05 '21 at 20:39
  • OMG you're right! I tried to access it using another network and it worked! You're a genius and I have no words to show my appreciation and gratitude for your help! I just have one last question, how do I keep it running in the background? – KiDo Oct 06 '21 at 06:09
  • You can follow my answer at this question to run it as systemd service: https://serverfault.com/questions/1078666/how-do-i-create-a-python-3-service-that-uses-socket-with-systemd/1079133#1079133 – Aleksandr Chendev Oct 06 '21 at 07:57