[Part-1]Linux Python 3 environment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Tutorials (https://python-forum.io/forum-4.html) +---- Forum: Python Installation and Execution (https://python-forum.io/forum-41.html) +---- Thread: [Part-1]Linux Python 3 environment (/thread-3408.html) |
[Part-1]Linux Python 3 environment - snippsat - May-21-2017 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: 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-packagesRunning 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:
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.2Running code: 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.0So 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] RE: [Part-1]Linux Python 3 environment - snippsat - May-25-2017 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:
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 https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv Add these 3 lines last in ~/.bashrc Example how to add lines:mint@mint ~ $ nano ~/.bashrc # paste in lines last # Ctrl-O <enter>(save) Ctrl-XRestart 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: So as a example want to test out miniconda pyenv install miniconda3-4.3.11 Setting up virtual environment with pyenv Install into environment:
|