documentation/Examples/Debian/Install Python.md

54 lines
1.6 KiB
Markdown

###### About
<https://www.python.org/>
###### Procedure
1. Sign in as root user
2. 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
3. Run `python3 --version` to identify the version of Python installed
###### New Projects
1. Run `mkdir myapp && cd myapp` to create a new home for our Python project
2. Run `python3 -m venv venv` to create a virtual environment (venv)
3. 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!
4. 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
1. Daemonize the Python application
1. Create a new shell script via `nano myapp.sh`
```sh title="/root/myapp/myapp.sh"
#!/bin/bash
cd /root/myapp
source ./venv/bin/activate
python -u myapp.py
```
1. Run `chmod +x myapp.sh`
2. Learn about [installing Node.js and PM2](?q=Node.js)
3. Run `pm2 start myapp.sh && pm2 save`