Part-1 is here,which contend basic install Python and use of
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
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.
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
Virtual Environments is a part of Python 3.6
A folder with full Python install(including pip),that don't mess with OS Python.
Running this code
So need to install BeautifulSoup, Requests, lxml into virtual environment.
Running code:
Make
if something break in future we can now see that with these versions it did work.
Can also install with it.
Overview image:
[Image: xE84P5.jpg]
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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 |
find_title.py
in virtual environment.So need to install BeautifulSoup, Requests, lxml into virtual environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# 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' find_title(url) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
(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 |
1 2 3 |
(my_env) C:\my_env λ python find_title.py Welcome to Python.org |
requirements.txt
for this virtual environment.1 2 |
(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]