“無效語法”錯誤首先發生。
lzh@ubuntu:~/Graduation_Project/Code$ bash test.sh
File "/home/lzh/Graduation_Project/Code/update_day.py", line 27
print(day_url,end='')
^
SyntaxError: invalid syntax
我嘗試洗掉 end=' '代碼并再次運行它。
但它回傳另一個錯誤:'No module named' 錯誤
Traceback (most recent call last):
File "/home/lzh/Graduation_Project/Code/update_day.py", line 4, in <module>
from urllib.request import urlopen
ImportError: No module named request
但它可以在終端上成功運行。為什么?
lzh@ubuntu:~/Graduation_Project/Code$ python /home/lzh/Graduation_Project/Code/update_day.py
https://vup.darkflame.ga/api/summary/2022/3/6 done
Python(3.6.9)代碼:
# coding: utf-8
#! /bin/env python3
from urllib.request import urlopen
import urllib
import pandas as pd
import time
tlist = time.localtime()
# column = ['date','income', 'pay_num','danmu']
try_times = 20
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
year = tlist[0]
month = tlist[1]
day = tlist[2]
List = []
day_list = [31,0,31,30,31,30,31,31,30,31,30,31]
if day == 1:
if month == 2:
if (year%4 == 0 and year%100!=0 or year%400==0):
day=29
else:
day=28
else:
day = day_list[month-1]
day_url = 'https://vup.darkflame.ga/api/summary/' str(year) '/' str(month) '/' str(day-1)
print(day_url,end='')
外殼腳本代碼:
#! /bin/bash
#! /bin/env python3
python '/home/lzh/Graduation_Project/Code/update_day.py'
系統版本:Ubuntu 18.04.4 LTS
uj5u.com熱心網友回復:
您的代碼是為 Python 3 撰寫的,但您使用 Python 2 解釋器運行它。
如果要使用python3而不是python,則需要指定:
#!/bin/sh
exec python3 '/home/lzh/Graduation_Project/Code/update_day.py'
那里有更好的exec性能(因為它sh 用 Python 替換自己,而不是產生一個子行程來執行 Python 解釋器)——exec如果python3不再是腳本的最后一行,就去掉。
(使用sh而不是bash因為您在這里所做的任何事情都沒有利用 bash 的功能,并且 sh 啟動速度更快)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/438994.html
