Python Programming Overview 1
Python Programming Overview 1
(1) OS supported by Python
Portability
- Python is supported on nearly every major OS, including Windows
- v2.7 installed by default by most Linux distribution (Debian, Ubuntu)
- Others OS
- AS/400, BeOS, MorpOS, MS-DOS, OS/2, and more
Caveats
- There may be OS-specific caveats
- For example, on Windows: carriage returns are treated differently, as are file paths
- Permissions and file I/O are other things to watch out for
(2) Why Choose Linux?
Linux vs. Windows
- Not an OS debate
- Significant portion of Internet-facing servers run some form of UNIX
- Python first released for a UNIX-based OS
Today’s Technology
- If you wanted to use Linux at home, you either had to dual boot or un the OS exclusively
- Dual booting often a hassle
- With virtualization, barrier to entry is far lower, and almost no rist of data loss
“Easy” Linux Distributions
- Installing Linux used to be a pain; now way easier
- Easiest Linux Distros to install and use quickly
- Ubuntu (Debian-based)
- Linux Mint (Ubuntu-based)
(3) Installation & Virtual Environment
Libraries & Dependencies
- Like other languages, Python has a package management system
- Other languages got this right to begin with; Python has had some struggles with it
- “Standard” Python package manager is “pip”
- Packages that pip installs are in PyPI, the Python Package Index.
Installation
- Python packages by default are installed system wide
- Makes dealing with version conflicts and dependencies a pain
- “virtualenv” is the solution
Virtualenv & virtualenvwrapper
- Virtualenv creates a virtual environment where a copy of python and pip are “copied” to, and any packages are localized to
- Can run any number of desired virtualenvs, each with its own set of installed packages
- Each virtualenv operates independently
- Virtualenvwrapper is a handy shell script that shortcuts many virtualenv functions for you
- Now you can install python packages without worrying about your system integrity
- Or conflicting package versions
(4) Excercises
- Python-pip, and virtualenvwrapper are already installed (Figure 1)
- If you are running Linux, install pip for Python if it’s not already installed
- After installing pip, use it to install virtualenvwrapper (should require root privileges)
- Set up virtualenvwrapper and create a new virtualenv called “test”
- Automatically put you inside the virtual environment you just created (test)
- From here all actions are localize inside the virtual environment
- Virtualenvs create new virtual environment in your home directory (Figure 2)
- hidden directory “.virtualenvs”
- Install pip inside the virtual environment (Figure 3)
- Run python inside virtualenvs
- Try to do HTTP request and check the HTTP response
- Look up JSON object using “r.json()” (Figure 4)
- There is no JSON object
- Look up HTML object using “r.text” (Figure 5)
- There is no JSON object
- Exit from virtual environment “deactivate” (Figure 6)
- Import request: no such module
- pip was not installed, and module was not copied
- List virtualenv “lsvirtualenv”
- Go in test virtualenv “workon test”
- Import request: no such module
- To set an environment variable everytime, register the path in the .bashrc file (Figure 7)
- virtualenvwrapper.sh
- Or run the script by typing the full path (Figure 8)
- /usr/local/bin/virtualenvwrapper.sh
0 Comments