Python on Linux

Install a Local Python from Source

sudo apt install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev

To install dependencies.

What the following does: download source code, unpack it, make directory ~/.localpython to install into, run the configuration file setting install going to install folder, compile, install compilation, create a virtualenv pointing to the install, switch to the virtualenv to use it:

mkdir ~/src

cd ~/src

$ wget http://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz

$ tar -zxvf Python-3.11.0.tgz

$ cd Python-3.11.0

$ mkdir ~/localpython3110

$ ./configure --prefix=$HOME/localpython3110 --enable-optimizations

https://realpython.com/installing-python/#how-to-build-python-from-source-code

$ make &&make altinstall

Set Up venv

$ ~/localpython3110/bin/python3.11 -m venv py3110_venv

$ source py3110_venv/bin/activate

$ sudo apt install python3-pip -y

$ pip install --upgrade pip

$ pip install tk pillow numpy astropy astroplan pandas pytz matplotlib scikit-learn

$ pip list

$ pip -V

$ which python

$ pip install --upgrade pip

$ pip freeze --local > requirements.txt

$ deactivate

$ rm -rf somename_env

$ pip install -r requirements.txt

Note the venv folder stores neither the Python installation nor your code for your project. It is only used to store version information about the Python installation used for your project.

virtualenv instead of venv

I ran into a not-so-obscure reason to use virtualenv instead of venv. If you ever want to serve a Flask app using Apache or some other production server, virtualenv creates a file called activate_this, which Apache can use to run the Flask app in the appropriate Python environment.

sudo apt install python3-pip -y

pip install virtualenv

python3 -m virtualenv py31012_virtualenv

source py31012_virtualenv/bin/activate

pip install --upgrade pip

Install Packages from Local Folder

pip download package1 package2 -d'~/src/python-packages'

pip install package1 package2 -f ~/src/python-packages --no-index

Python Path

To see the path to the current python installation:

python -c 'import os, sys; print(os.path.dirname(sys.executable))'

To see the path variable in Windows (readable format):

PS > $env:path -split ';'