Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Part-2]Python environment Windows
#1
Part-1 is here,which contend basic install Python and use of pip to install 3-party modules.

A little more about pip, wheel, package creation, virtual environment and setting up stuff like a better cmd.
Package creation will be in Part-3.

A much better cmd cmder.
Download to a folder eg C:\cmder
One setup that important is .\cmder.exe /REGISTER ALL
This make a shortcut link to cmder as in image under.
So from any folder i file explorer direct to cmder in that folder. 
[Image: kGAwLo.jpg]

PIP
The tool for installing Python packages from PyPi or GitHub,BitBucket and more.
pip is part of all Python version over 2.7.9(just remember to mark on under install).

Useful commands:
Output:
pip help (show commands) # Install and uninstall into Python pip install requests pip uninstall requests # Wheel(.whl) from Gohlke pip install lxml‑3.7.3‑cp36‑cp36m‑win32.whl pip uninstall lxml‑3.7.3‑cp36‑cp36m‑win32.whl --- Diffrent ways --- pip install git+https://github.com/cherrypy/cherrypy.git pip install -r requirements.txt # A demo later # Upgrade pip python -m pip 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.

Virtual Environments is a part of Python 3.6
A folder with full Python install(including  pip),that don't mess with OS Python.
C:\
# Make virtual environment
λ python -m venv my_env
 
# cd into the folder
C:\
λ cd my_env
 
# Activate environment,in Linux is source bin/activate
C:\my_env
λ C:\my_env\Scripts\activate.bat
 
# Now is folder active,you see (my_env)
(my_env) C:\my_env
 
# With cmder i can check that right python is used
λ which python
/c/my_env/Scripts/python
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) C:\my_env
# Check that pip in environment is used
λ pip -V
pip 9.0.1 from c:\my_env\lib\site-packages (python 3.6)

# install BeautifulSoup, Requests, lxml
(my_env) C:\my_env
λ pip install beautifulsoup4 requests lxml
Collecting beautifulsoup4
  Downloading beautifulsoup4-4.6.0-py3-none-any.whl (86kB)
    100% |████████████████████████████████| 92kB 803kB/s
Collecting requests
  Downloading requests-2.14.2-py2.py3-none-any.whl (560kB)
    100% |████████████████████████████████| 563kB 1.4MB/s
Collecting lxml
  Using cached lxml-3.7.3-cp36-cp36m-win32.whl
Installing collected packages: beautifulsoup4, requests, lxml
Successfully installed beautifulsoup4-4.6.0 lxml-3.7.3 requests-2.14.2
Running code:
(my_env) C:\my_env
λ python find_title.py
Welcome to Python.org
Make requirements.txt for this virtual environment.
(my_env) C:\my_env
λ pip freeze > requirements.txt
Output:
(my_env) C:\my_env λ more requirements.txt beautifulsoup4==4.6.0 lxml==3.7.3 requests==2.14.2
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: xE84P5.jpg]
Reply


Messages In This Thread
[Part-2]Python environment Windows - by snippsat - May-20-2017, 06:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Part-1]Linux Python 3 environment snippsat 1 20,623 May-25-2017, 05:14 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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