Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active May 4, 2020 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/89aa62c98bd60403fefe3ab1b6eb993e to your computer and use it in GitHub Desktop.
Save Integralist/89aa62c98bd60403fefe3ab1b6eb993e to your computer and use it in GitHub Desktop.
[Compiling Python] #python #compile #manual #install

DOCUMENTATION: https://devguide.python.org/setup/#macos-and-os-x

The python3-setuptools package provides the easy_install3 command, which we can use to install pip (not sure why it's not included with Python beta as it normally is with standard releases?)

apt-get update && \
apt-get install -y -q wget && \
apt-get install -y -q build-essential python3-setuptools && \
apt-get build-dep -y -q python3 && \
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0b1.tgz && \
tar xf Python-3.6.0b1.tgz && \
rm Python-3.6.0b1.tgz && \
cd Python-3.6.0b1/ && \
./configure && \
make && \
./python --version

Don't try to move the ./python executable into /usr/local/bin/ as it won't be able to locate the other libraries/modules it links to. If anyone knows how to solve that issue, then please let me know!

Python looks here for packages:

./python  " starts REPL

>>> import sys
>>> print('\n'.join(sys.path))

/usr/local/lib/python36.zip
/Python-3.6.0b1/Lib
/Python-3.6.0b1/build/lib.linux-x86_64-3.6

So if we install Jinja2 we can see where it's installed:

pip install jinja2
/usr/local/lib/python3.4/dist-packages | grep -i jin

Meaning we can now modify the `` environment variable to utilise that package.

export PYTHONPATH="$PYTHONPATH:/usr/local/lib/python3.4/dist-packages"

So now if we open a REPL again with the new Python we can import the package:

./python  " starts REPL

>>> import sys
>>> print('\n'.join(sys.path))

/Python-3.6.0b1
/usr/local/lib/python3.4/dist-packages      " see it's there <<
/usr/local/lib/python36.zip
/Python-3.6.0b1/Lib
/Python-3.6.0b1/build/lib.linux-x86_64-3.6

import jinja2  " this now works √

The downside to this approach is that the module might not be compatible with the new beta release of Python we've just installed

Dockerfile

FROM ubuntu:14.04

ADD . /app

# Set the debconf frontend to Noninteractive
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

RUN apt-get update && apt-get install -y -q wget curl apt-transport-https lsb-release ca-certificates

# Download and build Python3
# The `python3-setuptools` package provides the `easy_install3` command
RUN apt-get install -y -q build-essential python3-setuptools && apt-get build-dep -y -q python3 && wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0b1.tgz && tar xf Python-3.6.0b1.tgz && rm Python-3.6.0b1.tgz && cd Python-3.6.0b1/ && ./configure && make && ./python --version
RUN easy_install3 pip && pip install jinja2
ENV PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python3.4/dist-packages"

## References

# DOCUMENTATION:
# https://devguide.python.org/setup/#macos-and-os-x
# GET FILES...
#
# wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0a5.tgz
curl https://www.python.org/ftp/python/3.9.0/Python-3.9.0a5.tgz -o Python-3.9.0a5.tgz
# UNZIP FILES...
tar xzvf Python-3.9.0a5.tgz
# CONFIGURE BUILD INFORMATION...
cd Python-3.9.0a5
./configure --prefix=$HOME/python-3.9.0a5
# COMPILE PYTHON
make
make install
$HOME/python-3.9.0a5/bin/python3.9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment