Skip to content

Instantly share code, notes, and snippets.

@jaesbit
Last active September 21, 2017 09:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaesbit/68a98454499859a2ddc67c03773ba006 to your computer and use it in GitHub Desktop.
Save jaesbit/68a98454499859a2ddc67c03773ba006 to your computer and use it in GitHub Desktop.
Python 3.6.x installer for ubuntu
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Just python 3.6.X deploy and install for UBUNTU 16.04 or greater
Can be run in python 2 and 3
Research, linux_py36 Started by Aaron Giovannini (agiovannini)
This file was created at 12/junio/2017
"""
import os
import sys
import requests
import importlib
from pathlib import Path
from subprocess import call
from distutils.dir_util import copy_tree
from distutils.file_util import copy_file
CURR_DIR = os.getcwd()
def check_import(module_name, lib_name=None):
"""
Simple function to use in setup enviroments, to check if some special library is not installed and install with pip
:param module_name: module name to install with pip
:param lib_name: package import line, if not defined same as module_name
:return:
"""
if not lib_name:
lib_name = module_name
try:
i = importlib.import_module(lib_name)
except Exception as err:
os.system(
"{sys.executable} -m pip install {module_name}".format(sys=sys, module_name=module_name))
os.system("{sys.executable} {args}".format(
sys=sys, args=' '.join(sys.argv)))
exit(0)
check_import("requests")
check_import("logging")
import logging
import requests
logging.getLogger().setLevel(logging.DEBUG)
if os.geteuid() != 0: # Check for root privileges
raise RuntimeError("This tool need to be executed as root user")
release_data = None
try:
with open('/etc/os-release', 'r') as reader:
release_data = reader.readlines()
except Exception as err:
raise RuntimeError(
"can't open release data, maybe not running in ubuntu-linux")
# Check if running under linux-ubuntu
if release_data and "ubuntu" not in release_data[0].lower():
raise RuntimeError(
"Unsupported operating system, {0}".format(release_data))
elif not release_data:
raise RuntimeError("Release data not found, are using ubuntu-linux?")
os.system("""
add-apt-repository ppa:jonathonf/python-3.6 -y
apt-get update
apt-get install python3.6-tk python3.6 python3.6-dev libffi-dev git make \\
build-essential libssl-dev curl zlib1g-dev libbz2-dev libreadline-dev \\
libsqlite3-dev tk-dev tcl-dev python-pip python-virtualenv \\
software-properties-common python-setuptools python3-pip python python3 -y
# rm -f /usr/bin/python
curl https://bootstrap.pypa.io/get-pip.py | python2
""")
data = """#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
"""
with open("/usr/local/bin/pip3.6", "w") as st:
st.write(data)
os.system("python3.6 -m pip install pip --upgrade")
Path("/usr/bin/python3").unlink()
os.system(
"sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1")
os.system(
"sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 2")
# Ubuntu 16.04 now uses by default python3 with python 3.5
# In order to serve an easy way for python 3.6 will activate in python instead python3
Path("/usr/bin/python").unlink()
os.system(
"sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 1")
os.system(
"sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2")
os.system(
"sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 3")
lib_python3 = Path("/usr/lib/python3/")
def walk(path):
for inner in Path(path).iterdir():
if inner.is_dir():
for sub in inner.iterdir():
yield sub
else:
yield inner
for f in walk(lib_python3):
if "-35m-" in f.name:
if "_cffi_" in f.name:
continue # Need to compile for 3.6
copy_file(f.as_posix(), f.as_posix().replace("-35m-", "-36m-"))
# Well need to copy 3.5 libraries, and merge into python3 shared,
# and... need to rename from -35m- to -36m- making python3.6 supported
pt = Path("/usr/lib/python3.6/lib-dynload/")
if not pt.exists():
pt.mkdir(parents=True)
try:
copy_file('/usr/lib/python3.5/lib-dynload/_gdbm.cpython-35m-x86_64-linux-gnu.so',
'/usr/lib/python3.6/lib-dynload/_gdbm.cpython-36m-x86_64-linux-gnu.so')
except Exception as err:
pass
with open("/usr/local/bin/pip", "w") as st:
# For some reason pip for python2.7 was overwrited to python3.6
st.write(data.replace("python3.6", "python"))
with open("/usr/local/bin/pip3", "w") as st:
st.write(data.replace("python3.6", "python3"))
cffi_link = "https://bitbucket.org/cffi/cffi/get/tip.tar.gz"
cffi_tar = "/tmp/cffi.tar.gz"
with open(cffi_tar, "wb") as down:
down.write(requests.get(cffi_link).content)
# That link is for compile and download cffi for python3.6
os.system("""
cd /tmp
mkdir cffi
tar -zxvf {cffi_tar} -C ./cffi --strip-components=1
cd cffi
/usr/bin/pyton3.6 setup.py build_ext -f -i
/usr/bin/python3.6 setup.py install
rm -r /tmp/cffi*
""".format(cffi_tar=cffi_tar))
os.chdir(CURR_DIR)
@jaesbit
Copy link
Author

jaesbit commented Sep 13, 2017

  • If try to use cffi.FFI() raises Exception (cffi module was compiled for python 3.5) [Status] Pending to fix, researching how to solve
  • When python 2.7 is installed on ubuntu, if activates 3.6 for python, we had some exceptions that i'm researching to fix. currently need to change with update-alternatives until found fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment