Install Python From Source Code
You need use this method to install the latest Python if you cannot upgrade python via “apt-get, yum”, for example, Friendly Arm”.
A common problem like this:
#https://unix.stackexchange.com/questions/332641/how-to-install-python-3-6
I’d like to install the latest Python, which is 3.6 at the time of this post. However, the repository is saying that Python 3.4.2 is the newest version.
I’ve tried:
sudo apt-get update sudo apt-get install python3 python3 is already the newest version. python -V Python 3.4.2
Some package need to be installed befor install Python to avoid some known problems
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev sudo apt-get install -y libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm sudo apt-get install -y libncurses5-dev libncursesw5-dev xz-utils tk-dev
It is recommended to use make altinstall
according to the official website.
Warning:
make install
can overwrite or masquerade the python binary.make altinstall
is therefore recommended instead ofmake install
since it only installsexec_prefix/bin/pythonversion
.
wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz tar xvf Python-3.6.3.tgz cd Python-3.6.3 ./configure --enable-optimizations make -j8 sudo make altinstall python3.6
Install pip
wget https://bootstrap.pypa.io/get-pip.py sudo python get-pip.py
WebSocket
websockets
requires Python ≥ 3.4. Install it with:
pip install websockets
async
and await
aren’t available in Python < 3.5. Here’s how to adapt the client example for older Python versions.
http://websockets.readthedocs.io/en/stable/intro.html
Function
Pass by Reference vs Value
All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function.
#!/usr/bin/python3 # Function definition is here def changeme( mylist ): "This changes a passed list into this function" print ("Values inside the function before change: ", mylist) mylist[2]=50 print ("Values inside the function after change: ", mylist) return # Now you can call changeme function mylist = [10,20,30] changeme( mylist ) print ("Values outside the function: ", mylist)
Compare to this:
#!/usr/bin/python3 # Function definition is here def changeme( mylist ): "This changes a passed list into this function" mylist = [1,2,3,4] # This would assi new reference in mylist print ("Values inside the function: ", mylist) return # Now you can call changeme function mylist = [10,20,30] changeme( mylist ) print ("Values outside the function: ", mylist)
Global vs. Local variables
Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.
total=0 def sum(arg1,arg2): total=arg1+arg2 print("Inside the function local total: ",total) return total sum(10,20) print("Outside the function global total: ",total)
Results:
D:\home\training\PytonTutorial λ python.exe functionPractice.py Inside the function local total: 30 Outside the function global total: 0
Compare to another one:
‘
total=0 def sum(arg1,arg2): global total total=arg1+arg2 print("Inside the function local total: ",total) return total sum(10,20) print("Outside the function global total: ",total)
Results:
D:\home\training\PytonTutorial λ python.exe functionPractice.py Inside the function local total: 30 Outside the function global total: 30
Reference:
- How to install python3.6, https://unix.stackexchange.com/questions/332641/how-to-install-python-3-6
- websocket, http://websockets.readthedocs.io/en/stable/intro.html
- RTD