- check python version
python --ver
orpython3 --ver
- to enter python
python
orpython3
orpython3.<version>
orpython2.<version>
- after enter to python, type help() to get help
>>>help()
orhelp('modules')
orhelp('<modules_name>') e.g. help('paramiko') or help('<modules_name>.<class_name>') e.g. help('getpass.getpass')
also without entering python, can use:
pydoc <moudule> or pydoc <module>.<class> - to list the modules
help>modules
- Create virtual environment: install using pip
pip install virtualenv
orpip3 install virtualenv
Create virtualenv directory:virtualenv <directory_name>
Activate virtualenv:source <directory_name>/bin/activate
The prompt now will begin with <directory_name>. - Virtual environment to use python 2.7
virtualenv -p /usr/bin/python2.7 <new_venv>
- input
var_as_st = raw_input('some words')
but in python 3.x, we need to add int before the input:
var_python_will_chose_best_type = input('some words')var = int(input("something"))
also not available anymore in python3.x
raw_input - Output
python2.x :print var
python3.x :print(var)
if need to decode the ascii:print(var.decode(‘ascii’)
- Write
python2.x:var.write(“something”)
python3.x:var.write(b”something”)
where b means byte
to read from input:
python3.x:var.write(var_input.encode(‘ascii’))
- Comment: use #
- Variable type:
>>>type(var)
- List of operation to a variable:
>>>dir(var)
- PEP: Python Enhancement Proposals
- single quote(‘) and double quote(“) are same
- indentation: use 4 spaces. e.g. all lines after IF with indentation are clause if IF true (or false)
- be consistent: tab, then all tab. Space then all spaces
Source: Youtube
Error
- ImportError: cannot import name
This is if the filename is same as library/module name.
$ python2.7 python/telnetlib.py
Traceback (most recent call last):
File "python/telnetlib.py", line 2, in <module>
from telnetlib import Telnet
File "/home/damha/python/telnetlib.py", line 2, in <module>
from telnetlib import Telnet