General
Python is a scripting language.
Scripts are any *.py file executed by an interpreter (like Python’s IDLE). Package is a collection of modules/scripts installed by a package manager.
Python in Windows
PATH is an environment variable in Windows. It basically tells the commandline what folders to look in when attempting to find a file. If you didn’t add Python to PATH then you would call it from the commandline like this:
C:/Python27/Python some_python_script.py
Whereas if you add it to PATH, you can do this:
python some_python_script.py
- Open command prompt
- Enable Python environment (ensure Python installed beforehand): python
- This will show up: >>>
Sample code with Python in Command Prompt:
>>> print(‘Hello World!’) Hello World! >>> 2 + 57 59 |
To exit Python environment: quit() or exit() or ctrl+z (and enter)
To run *.py (e.g. abc.py), | |
In Command Prompt, :: Exit Python environment (if in it) :: Go to directory of abc.py :: Type and press enter: python abc.py | In IDLE, :: Open abc.py in IDLE :: Run (press F5) |
To find where python is installed, execute in cmd: where python
# save as xz.pydef add(x, z): result = x + z return result | # save as addxz.pyimport xz xz.add(3,4) # when run addxz.py , output will be 7 |
Check python version: python –version
If version still shows the previous, update to latest @ PATH by Moving Up.
Python’s Packages
PIP
PIP is Python’s package manager.
# Check PIP version
pip --version
# Download and install packages like TensorFlow and camelcase
pip install <packagename>
# Using package
import camelcase
c = camelcase.CamelCase()
Package
# Useful packages list
pip install jupyterlab # jupyterlab; launch with: jupyter-lab
pip install notebook # classic Jupyter notebook; launch with: jupyter notebook
Unknowns
venv / pyvenv | single Python version |
pyenv | multiple Python version (deprecated @ Python 3.8+ and replaced with venv) |
pyenv-virtualenv | Python 2 inclusive |
venv
$ python3 -m venv directory-name-to-create
# e.g. python3.6 -m venv example-three-six
$ source name-given/bin/activate
$ deactivate
$ rm -r name-given
When the environment is active, any packages can be installed to it via pip as normal (by default, the newly created environment will not include any packages).