Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Part-1]Linux Python 3 environment
#1
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:
Output:
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.py
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('https://www.python.org/')
>>> r.status_code
200
>>> r.encoding
'utf-8'
>>> r.headers['date']
'Sun, 21 May 2017 13:42:43 GMT'

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

Useful commands:
Output:
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+https://github.com/cherrypy/cherrypy.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
/home/mint/my_env/bin/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 find_title.py in virtual environment.
So need to install BeautifulSoup, Requests, lxml into virtual environment.
# find_title.py
import requests
from bs4 import BeautifulSoup

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

if __name__ == '__main__':
    #url = 'http://CNN.com'
    url = 'https://www.python.org/'
    find_title(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:
Output:
(my_env) mint@mint ~/my_env $ python3 find_title.py Welcome to Python.org
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
appdirs==1.4.3
beautifulsoup4==4.6.0
lxml==3.7.3
packaging==16.8
pkg-resources==0.0.0
pyparsing==2.2.0
requests==2.14.2
six==1.10.0
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]
Reply


Messages In This Thread
[Part-1]Linux Python 3 environment - by snippsat - May-21-2017, 10:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Part-2]Python environment Windows snippsat 4 25,163 May-24-2017, 10:44 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020