Python is already on your service. For most scripts you only need your own packages, and a virtual environment gives you those without root access (step 2). If a program needs a newer Python than the one on the server, pyenv installs it in your home folder (step 3). Connect over SSH first: SSH how to get connected.
1. The Python on Your Service
python3 --version
Python 3.10.12
Use python3. The command python starts the old Python 2.7, which most programs no longer support.
2. Your Own Packages: a Virtual Environment
A virtual environment is a folder with its own copy of pip and its own packages. Create one for each program:
python3 -m venv ~/venvs/myscript ~/venvs/myscript/bin/pip install requests
Then start your script with the Python of that folder, and it finds the packages:
~/venvs/myscript/bin/python ~/myscript.py
Use the same full path in cron: Run a Command on a Schedule With cron on App Hosting. To remove the environment, delete its folder: rm -r ~/venvs/myscript.
3. A Newer Python With pyenv
pyenv builds Python from its source code on your service. The server lacks the files that the modules sqlite3, bz2, lzma and readline need, so they are left out. Programs that keep their data in an SQLite database then do not start. If your program needs sqlite3, use the Python of the server (step 2).
3a. Install pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Add pyenv to your shell. Copy all lines at once:
cat >> ~/.bashrc << 'EOF' export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init - bash)" EOF
Log out of SSH and log in again. pyenv --version now answers with the version of pyenv.
3b. Install a Python Version
See which versions you can install, then install one. The number 3.14 takes the newest 3.14.x:
pyenv install --list | grep -E "^ *3\.1[3-5]\.[0-9]+$" nice -n 19 pyenv install 3.14
The build takes a minute or two and keeps the processor of the server busy; nice -n 19 lets your apps go first. At the end, pyenv lists the missing modules:
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib? WARNING: The Python readline extension was not compiled. Missing the GNU readline lib? WARNING: The Python sqlite3 extension was not compiled. Missing the SQLite3 lib? WARNING: The Python lzma extension was not compiled. Missing the lzma lib? Installed Python-3.14.10 to /home2/alexdemo/.pyenv/versions/3.14.10
3c. Use It
pyenv global 3.14.10 python --version
Python 3.14.10
From now on python, python3 and pip are the new version. Create a virtual environment with it as in step 2, with python -m venv instead of python3 -m venv.
| Command | What it does |
|---|---|
pyenv versions | Lists the installed versions; * marks the one in use. |
pyenv global system | Goes back to the Python of the server. |
pyenv local 3.14.10 | Uses this version only in the current folder and its subfolders. |
pyenv uninstall 3.14.10 | Removes a version. |
In cron and in scripts, use the full path, because they do not read ~/.bashrc: ~/.pyenv/versions/3.14.10/bin/python.
3d. Remove pyenv
rm -rf ~/.pyenv
Then delete the three lines from ~/.bashrc with nano ~/.bashrc, and log out and in again.
Good to Know
- Programs that need a newer Node.js instead: Node.js: Install It in Your Home Folder.
- Long jobs keep running after you close SSH when you start them in tmux: tmux: Keep a Command Running After You Close SSH.