I made a python file that can be executed after installing python3 even in an environment without python3.
It is assumed that it is unlikely that anyone can do anything as root in the development environment.
How to write the correct shebang in Perl, Python and Ruby scripts You can write more than one shebang! ?? I searched for it. It's just Don Pisha, and without this article I wouldn't have done such a stupid thing.
[How to write Ruby that can be executed as PHP] It's a different idea, but I was inspired by the fact that one file is for multiple languages. I want to try this too.
install_python3.py
#!/bin/sh
# -*- coding: UTF-8 -*-
""":"
#Write sh here
#Installation & decompression of installation version and compressed files
tmp='/tmp/'
pythonver='3.4.3'
binarypath=$tmp'Python-'$pythonver
#Place of installation
lib='/usr/lib/'
srcpath=$lib'python'$pythonver
#python3 symbolic link installation location
bin='/usr/bin/'
lnname='python3'
if [ -f $bin$lnname ]; then
#If you have python3, just run python
echo 'No installation required'
exec $lnname "$0" ${1+"$@"}
fi
echo 'Installation required'
if [ -f $binarypath'.tgz' ]; then
echo $binarypath'.tgz already exists'
else
echo $binarypath'.Download tgz'
wget https://www.python.org/ftp/python/${pythonver}/Python-${pythonver}.tgz -P ${tmp}
fi
if [ -d $binarypath ]; then
echo $binarypath'Is already there'
else
echo $binarypath'Expand to'
tar zxf $binarypath'.tgz' -C $tmp
fi
if [ -d $srcpath ]; then
echo $srcpath'Installed in'
else
echo $srcpath'Install to'
#If you cd, it will be moss in later python execution, so return after installation
pwd=`pwd`
cd $binarypath && ./configure --prefix=${srcpath} && make && make install
cd $pwd
fi
if [ -f $bin$lnname ]; then
echo $bin$lnname'Already installed'
else
echo $bin$lnname'To install'
ln -s $srcpath'/bin/python3' $bin$lnname
fi
exec $lnname "$0" ${1+"$@"}
"""
#From here python
import sys
version = sys.version_info
if version[0] == 2:
print('2')
else:
print(version[0])
At first, if the version of python
was 2, I replaced python
with 3, but I stopped it because it seemed to affect other things and in fact yum stopped working.
The example of failure below is a remnant of that.
** Dynamic languages are analyzed and executed line by line with an interpreter, right? ** ** ** Then it's okay if you don't go through the problematic part? ** **
When I use it normally, I always realize that it doesn't happen, but once I can achieve the purpose, I think it's convenient for me.
** Version determined in python and tried to update python in python **
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import tarfile
version = sys.version_info
if version[0] == 2:
print 'Update to 3 for version 2'
if not os.path.isfile('/tmp/Python-3.4.3.tgz'):
print 'Download'
os.system('wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tgz -P /tmp')
else:
print '/tmp/Python-3.4.3.tgz already exists'
# 2.Because it is 4, extract all is nice
if not os.path.isdir('/tmp/Python-3.4.3'):
print '/tmp/Python-3.4.Expand to 3 and install'
tf = tarfile.open('/tmp/Python-3.4.3.tgz', 'r')
tf.list()
for item in tf:
tf.extract(item, '/tmp/')
#Build
if not os.path.isdir('/usr/lib/python3.4.3'):
os.system('cd /tmp/Python-3.4.3/ && ./configure --prefix=/usr/lib/python3.4.3 && make && make install')
else:
print '/usr/lib/python3.4.Installed on 3'
else:
print '/tmp/Python-3.4.Expanded to 3'
# /usr/bin/python replacement
if not os.path.isfile('/usr/bin/python.old'):
os.rename('/usr/bin/python', '/usr/bin/python.old')
os.symlink('/usr/lib/python3.4.3/bin/python3.4', '/usr/bin/python')
#Existing/usr/bin/python2 is a symbolic link to python, so update it
#Overwriting is unreasonable, so delete it once
os.remove('/usr/bin/python2')
os.symlink('/usr/bin/python2.4', '/usr/bin/python2')
else:
print '/usr/lib/python has been replaced'
sys.exit()
#This should be done in case of 3
print('test')
At the time of python2, print ('test')
was safe, so I thought "I can go!", But when I raised it to 3, after all it was print
and syntax error
It seems that both print
andprint ()
can be used in python2 ...
I was spurred on my belief (passing responsibility)
Well, before I started writing, I just read PHP runtime and compile time and realized that it was just impossible.
php Even if it looks like you specify the source code file in a command and execute it directly like hoge.php, modern interpreters parse the source code and then compile it into virtual machine bytecode. Ruby, Python, Perl5, Emacs as well.
** But I just thought that python3, which does not use print
on the python2 side and protects syntax
, may still work !? **.
I have no plans to use it.
Recommended Posts