Python Forum

Full Version: [Part-1]Linux Python 3 environment
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This will be similar to starting Python in Windows Part-1 Part-2.
The main focus will be running Python 3.5 or higher on Linux.
It's a open tutorial,so if someone has useful information post it here.

I have tested with Mint 18.1 and Ubuntu 16.04(run at Digital Ocean).
Both of these Distros come with Python 3.5 as default Python 3 version.

Basic install:
sudo apt-get update sudo apt-get install build-essential sudo apt-get install python3-dev sudo apt install python3-pip sudo pip3 install virtualenv
Testing version:
mint@mint ~ $ python3 -V
Python 3.5.2
mint@mint ~ $ python -V
Python 2.7.12

mint@mint ~ $ pip3 -V
pip 9.0.1 from /usr/local/lib/python3.5/dist-packages (python 3.5)

# Upgrade pip
mint@mint ~ $ pip3 install -U pip
Requirement already up-to-date: pip in /usr/local/lib/python3.5/dist-packages
Running script with Python 3.5 and install with pip3:
# Running code
mint@mint ~ $ python3
hello world

# Install Requests
mint@mint ~ $ sudo pip3 install requests
Collecting requests
  Downloading requests-2.14.2-py2.py3-none-any.whl (560kB)
    100% |████████████████████████████████| 563kB 1.1MB/s 
Installing collected packages: requests
Successfully installed requests-2.14.2

# Test that Requests work
mint@mint ~ $ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> r = requests.get('')
>>> r.status_code
>>> r.encoding
>>> r.headers['date']
'Sun, 21 May 2017 13:42:43 GMT'

The tool for installing Python packages from PyPi or GitHub,BitBucket and more.

Useful commands:
pip help (show commands) # Install and uninstall into Python pip install requests pip uninstall requests # Wheel(.whl)  pip install some_package.whl pip uninstall some_package.whl --- Diffrent ways --- pip install git+ pip install -r requirements.txt # A demo later # Upgrade pip pip3 install -U pip # Show version pip -V  # Show installed packages  pip list  pip freeze # search PyPi for packages with term "flask" in it  pip search flask

Virtual environment
The main purpose of Python virtual environments is to create an isolated environment for Python projects.
This means that each project can have its own dependencies, regardless of what dependencies every other project has.
These dependencies can be written to a requirements.txt which we look at later.

Did install it first in post sudo pip3 install virtualenv
From Python 3.6 will be build in,for Python 3.5 using 3-party Virtualenv.
# Make virtualenv that use Python 3.5
mint@mint ~/Desktop/my_env $ virtualenv -p /usr/bin/python3.5 my_env
mint@mint ~ $ cd my_env

# Activate virtualenv
mint@mint ~/Desktop/my_env $ source bin/activate

# Check python loaction
(my_env) mint@mint ~/my_env $ which python

# pip version
(my_env) mint@mint ~/my_env $ pip -V
pip 9.0.1 from /home/mint/my_env/lib/python3.5/site-packages (python 3.5)
Running this code in virtual environment.
So need to install BeautifulSoup, Requests, lxml into virtual environment.
import requests
from bs4 import BeautifulSoup

def find_title(url):
    url_get = requests.get(url)
    soup = BeautifulSoup(url_get.content, 'lxml')
    print('head > title')[0].text)

if __name__ == '__main__':
    #url = ''
    url = ''
(my_env) mint@mint ~/my_env $ pip install beautifulsoup4 requests lxml
Collecting beautifulsoup4                                                       
Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86kB)                                   
    100% |████████████████████████████████| 92kB 821kB/s 
Collecting requests                                                                              
  Downloading requests-2.14.2-py2.py3-none-any.whl (560kB)                                            
    100% |████████████████████████████████| 563kB 1.2MB/s 
Collecting lxml                                                                                               
  Downloading lxml-3.7.3-cp35-cp35m-manylinux1_x86_64.whl (7.1MB)                                             
    100% |████████████████████████████████| 7.1MB 233kB/s 
Installing collected packages: beautifulsoup4, requests, lxml
Successfully installed beautifulsoup4-4.6.0 lxml-3.7.3 requests-2.14.2
Running code:
(my_env) mint@mint ~/my_env $ python3 Welcome to
Make requirements.txt for this virtual environment.
(my_env) mint@mint ~/my_env $ pip freeze > requirements.txt
(my_env) mint@mint ~/my_env $ cat requirements.txt
So we see which version that are used,
if something break in future we can now see that with these versions it did work.
Can also install with it.
pip install -r requirements.txt

Overview image:
[Image: 7KAU5q.jpg]
Over have looked at using default Python 3 installation that linux Distros has.
Now look Simple Python Version Management: pyenv

Using a new versions of Python is simple with pyenv.
pyenv install 3.6.1 to install newest version.
It's fully automated for both install and virtual environment pyenv virtualenv 3.6.1 env_36
Everything runs as your user,so you don't have to worry about messing up the Python used by linux Distros itself.
Look at a run:
mint@mint ~ $ pyenv install 3.6.1 Downloading Python-3.6.1.tar.xz... -> Installing Python-3.6.1... Installed Python-3.6.1 to /home/mint/.pyenv/versions/3.6.1 # Make it default version mint@mint ~ $ pyenv global 3.6.1 mint@mint ~ $ python Python 3.6.1 (default, May 25 2017, 14:44:39) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>>

Setting up pyenv:
Install headers needed to build CPython:
sudo apt-get install -y build-essential libbz2-dev libssl-dev libreadline-dev libsqlite3-dev tk-dev
Optional scientific package headers(Numpy, Matplotlib, SciPy, etc.)
sudo apt-get install -y libpng-dev libfreetype6-dev

Run the installer script (installs pyenv and some very useful pyenv plugins).
git clone $(pyenv root)/plugins/pyenv-virtualenv

Add these 3 lines last in ~/.bashrc
export PATH="~/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"
Example how to add lines:
mint@mint ~ $ nano ~/.bashrc
# paste in lines last 
# Ctrl-O <enter>(save) Ctrl-X
Restart shell.

Setting up environment:
Looked at example run first with pyenv install 3.6.1,
and making it global pyenv global 3.6.1
Here a sample of --list available versions:
mint@mint ~ $ pyenv install --list Available versions: 3.6.1 3.7-dev anaconda3-4.3.1 miniconda3-4.3.11 ironpython-2.7.7 jython-2.7.1b3 pypy3.5-5.7.1-beta-src pypy3.5-5.7.1-beta pypy3-portable-5.7.0
So as a example want to test out miniconda pyenv install miniconda3-4.3.11

Setting up virtual environment with pyenv 
mint@mint ~ $ pyenv virtualenv 3.6.1  env_36 # Activate environment mint@mint ~ $ pyenv activate env_36 (env_36) mint@mint ~ $ pip -V pip 9.0.1 from /home/mint/.pyenv/versions/3.6.1/envs/env_36/lib/python3.6/site-packages (python 3.6)
Install into environment:
(env_36) mint@mint ~ $ pip install requests Collecting requests Installing collected packages: requests Successfully installed requests-2.14.2 # Test that it work (env_36) mint@mint ~ $ python Python 3.6.1 (default, May 25 2017, 14:44:39) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> r = requests.get('') >>> r <Response [200]> >>> r.headers['date'] 'Thu, 25 May 2017 16:59:22 GMT'