0

'invalid syntax' error occurs first.

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

I try to delete end=' 'code and run it again.

but it return another error: 'No module named' error

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

but it can run on Terminal successfully. Why?

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) code:

# 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='')

shell script code:

#! /bin/bash
#! /bin/env python3
python '/home/lzh/Graduation_Project/Code/update_day.py'

system version:Ubuntu 18.04.4 LTS

4
  • 1
    Is it a possibility that you have two different versions of Python installed on your machine? Commented Mar 7, 2022 at 14:58
  • Change from python to python3. The #! /bin/env python3 is not doing anything at all. Commented Mar 7, 2022 at 15:01
  • (Also, a shebang is properly written #!/bin/bash, without a space) Commented Mar 7, 2022 at 15:01
  • BTW, why are you doing this in the first place instead of just making a symlink? Commented Mar 7, 2022 at 15:05

1 Answer 1

1

Your code is written for Python 3, but you're running it with a Python 2 interpreter.

If you want to use python3 instead of python, you need to specify that:

#!/bin/sh
exec python3 '/home/lzh/Graduation_Project/Code/update_day.py'

The exec is there for better performance (because it makes sh replace itself with Python instead of spawning a subprocess to exec the Python interpreter in) -- take exec out if python3 is ever no longer the last line of your script.

(using sh instead of bash because nothing you're doing here takes advantage of bash's features, and sh is faster to start).

Sign up to request clarification or add additional context in comments.

2 Comments

I have changed my Ubuntu default Python version to 3.6.9. So I think it is running on Python3. I'm not good at Shell script. And Thank you so much!
How you changed the default matters. If you did it with an alias, f/e, that only impacts your interactive shell; it doesn't change behavior of scripts.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.