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
#2
I think there should be a distinct part that says you dont have to use environments. This is how to install to your OS python and this how to install via environment,etc. Some users could care less about them...especially when they are just trying to get their tutorials to run. Here it implies that you must use one.
Recommended Tutorials:
Reply
#3
(May-23-2017, 10:33 AM)metulburr Wrote: Some users could care less about them

Ok, I confess! I have never used a virtual environment, though it is nice to see an example of how to do it, should I ever find the need.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
I have update with purpose of virtual environment for both Windows and Linux tutorial.
An describe better that Part-1 has the basic stuff.
Reply
#5
(May-23-2017, 02:17 PM)sparkz_alot Wrote: Ok, I confess! I have never used a virtual environment, though it is nice to see an example of how to do it, should I ever find the need.
I have used them. But i fail to see the need. And i have never needed to use different library versions simultaneously on different programs. Usually the latest is fine. And i usually prefer tuts that dont use environments as i like to see it raw on the system. It adds an extra layer of complexity when your just trying to understand the tutorial alone. Especially if you have never used one before. Then you end up spending most of your time dicking with the environment and/or converting it to your OS Python than actually understanding the tutroial. I bet those that dont know about pip and how to install a python 3rd library via pip could care less about an environment. Though the transition is easy in our eyes to use it on your OS Python, those just starting out have no idea.

im not knocking your method snippsat, im just trying to think of it in terms of a new user. I dont think most would follow suit and install cmder or use an environment.
Quote:(my_env) C:\my_env
λ pip install beautifulsoup4 requests lxml

this would look foreign to me. For one because most likely a new user would not use an environment. Most have never even used the terminal/command line before ever. And second no new user starts off with cmder. Though it is nice, new users would look at it like it looks nothing like mine. Out of all that all they really need to know is
pip install beautifulsoup4 requests lxml
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Part-1]Linux Python 3 environment snippsat 1 20,541 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