Linux Python

      在〈Linux Python〉中尚無留言

Python安裝

Ubuntu 18.04已預設安裝python3了, 所以不需安裝. 但並沒有安裝 pip3, 所以需下達下面指令安裝

sudo apt-get install python3-pip

安裝Pycharm

首先下載Linux版的Pycharm https://www.jetbrains.com/pycharm/download/#section=linux
使用 tar zxvf xxx.tar.gz解開即可, 然後在桌面編寫 pycharm.desktop

[Desktop Entry]
Name=pycharm
Comment=open pycharm IDE
Exec=sh /home/thomas/pycharm-community-2020.1.2/bin/pycharm.sh
Icon=/home/thomas/pycharm-community-2020.1.2/bin/pycharm.png
Terminal=false
Type=Application

最後在桌面圖示按右鍵/屬性/權限/允許檔案作為程式執行

QtDesigner for Linux

sudo apt-get install qt5-default
sudo apt-get install qttools5-dev-tools
sudo pip3 install pyqt5

安裝好之後,可以使用以下指令打開 Qt Designer:

designer

執行

python3 xxx.py

開機自動執行python程式

在 /etc/profile.d 下新增如下 myapp.sh

/home/pi/Resume/MainFrame.py &

1. 請記得最後要加 &
2. MainFrame.py 第一行要加 #!/usr/bin/python3

3. 要把MainFrame.py 改為755
4. myapp.sh不一定要改為755

未登入即自動執行

若希望在尚未登入帳號前就自動執行, 則將要執行的指令加在 /etc/rc.local 內

將 .py變為執行檔

在 .py檔最前面加入如下代碼

#!/usr/bin/python3

numpy

sudo pip3 uninstall numpy
sudo apt install python3-numpy

MySQL套件

sudo pip3 install mysql-connector-python

PyQt5套件

sudo apt-get install python3-pyqt5 qt5-default qttools5-dev-tools python3-matplotlib python3-plotly
sudo pip3 install plotly-express

wxPython套件

sudo apt-get install python3-wxgtk4.0

pynput

sudo pip3 install pynput

換行

Linux的換行跟 Windows不一樣, 所以在Pycharm下所寫的程式碼, 若想要在Linux下執行, 需於Pycharm的Settings/Editor/Code Style下, 把Line separator改為 Unix and macOS(\n)

barcode

想產生barcode圖像, 需安裝python-barcode, 但安裝此套件前, 需先安裝 Pillow及pathlib

sudo pip3 install Pillow pathlib python-barcode

寫入pdf檔

sudo pip3 install reportlab
from reportlab.pdfgen import canvas
c=canvas.Canvas("tmp.pdf", p)
c.drawString(0,0,"hello world")
c.showPage()
c.save()

pdf中英文字型

測試出文泉字型在顯示中英文都沒問題, 所以先安裝此字体

sudo apt-get install ttf-wqy-zenhei

字体目錄位於

/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc

檔案目錄維護

直播伺服器的影像存放目錄位於  /data/record, 六支錄影器全都放於此, 若想依Camera, 日期存放, 可使用下面代碼

#!/usr/bin/python3
import os
root_dir="/data/record"
ls=os.listdir(root_dir)
for l in ls:
    if os.path.isdir(f"{root_dir}/{l}"):
        continue
    fs=l.split('-')
    video_dir=fs[0]
    date_dir=f"{fs[2]}-{fs[3]}-{fs[4]}"

    source_file=f"{root_dir}/{l}"
    target_dir = f"{root_dir}/{video_dir}/{date_dir}"
    target_file=f"{target_dir}/{l}"

    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    if os.stat(source_file).st_size==0:
        print(f"delete {source_file}")
        os.remove(source_file)
    else:
        print(f"mv {source_file} {target_file}")
        os.rename(source_file, target_file)

pyautogui

此套件可以模擬滑鼠或鍵盤被按下的事件. 通常是用來作自動化測試用的

sudo pip3 install pyautogui

在Linux下, 若是ubuntu mate, 還需安裝如下套件才可使用

sudo apt-get install python3-tk python3-dev

上述套件若是在raspbian的系統, 因系統已自動安裝好了, 所以不需手動安裝

然後在python可以使用如下方式模擬鍵盤

import pyautogui
if btn.text()=='del':
    pyautogui.press("backspace")
elif btn.text()==".":
    pyautogui.press(".")
else:
    pyautogui.press(btn.text())

opencv

sudo apt-get install python3-opencv
import cv2
import time
cam=cv2.VideoCapture(0)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
while True:
    start = time.time()
    ret, frame=cam.read()
    if ret:
        #end = time.time()
        #seconds = end - start
        #print("Time taken : {0} seconds".format(seconds))
        #fps = 1 / seconds;
        #print("Estimated frames per second : {0}".format(fps));

        cv2.imshow('frame', frame)
    else:
        print("error----------------------")
    if 0xFF & cv2.waitKey(5)==27:
        break;
cv2.destroyAllWindows()

Linux Server 爬蟲

在Linux之下,需先安裝google-chrome-stable套件,然後再安裝webdriver

安裝google-chrome-stable的指令如下

sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
sudo apt-get -y update
sudo apt-get -y install google-chrome-stable

然後再下載 webDriver for Linux 64的版本到專案之下,即可開始執行。

Linux特定時間自動執行

在/etc/cron.d之下新增 autostock檔案,即可在每日晚上 9:00自動執行爬蟲程式

#每日晚上 9:00自動執行
0 21 * * * thomas /data/server/auto/crawler/twstock.py >> /data/server/auto/log_stock.txt

然後要下達如下指令啟動 cron服務

sudo service cron start#啟動 cron
sudo service cron reload#重新載入服務

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *