1.6 KiB
1.6 KiB
About
Procedure
-
Sign in as root user
-
Run
apt install python3 python3-venv
to install Python 3.x, whichever version may be the latest!!! info "Info"
The package `python3-venv` is included because it is in best practice to containerize Python projects and keep their dependencies isolated in environments where more than one application may be hosted
-
Run
python3 --version
to identify the version of Python installed
New Projects
-
Run
mkdir myapp && cd myapp
to create a new home for our Python project -
Run
python3 -m venv venv
to create a virtual environment (venv) -
Run
source ./venv/bin/activate
to activate!!! info "Info"
Calling `python3` or `pip3` is no longer necessary. The alias `python` is symbolically linked to the correct executable and a copy of pip is included within the virtual environment. Installing packages using `pip install -r requirements.txt` or `pip install x` will install the downloaded libraries and modules local to this environment!
-
Run
python -u myapp.py
to run a script using our venv!!! tip "Tip"
The `-u` in `python -u myapp.py` is instructing the Python interpreter to run in unbuffered output mode
Optional
-
Daemonize the Python application
-
Create a new shell script via
nano myapp.sh
#!/bin/bash cd /root/myapp source ./venv/bin/activate python -u myapp.py
-
Run
chmod +x myapp.sh
-
Learn about installing Node.js and PM2
-
Run
pm2 start myapp.sh && pm2 save
-