I tried to schedule the following file to run within my virtual environment (I wrote a similar task before and that one works fine, the only difference it the file name):
<p>/home/username/.virtualenvs/myvirtualenv/bin/python /home/username/folder/code.py</p>
<br>
I received an error message says
<p>[Errno 2] No such file or directory, Completed task, took 8.00 seconds, return code was 2. </p>
<p>However, this file is running properly when I run it manually in PythonAnywhere.</p>
<br/>
<p>In my code, I first call the API, and create a temp csv file to store the data, which will be deleted right after I create the data frame. Then it creates a data frame to transfer the selected data from the csv to the Google Sheet.</p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 | #!/usr/bin/python3.7
import sys, os
from apiclient import discovery
from google.oauth2 import service_account
import requests
import pandas as pd
from time_env import *
app_id = 'idxxxxxxx'
report_type = 'report_name'
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
params = {
'api_token': '8xxxx-xxxx-xxxx-xxxx-xxxx',
'from': beg_mo_date(), #from time_env
'to': yest_date()
}
request_url = 'https://hq.api_name.com/export/{}/{}/v5'.format(app_id, report_type)
res = requests.request('GET', request_url, params=params)
if res.status_code != 200:
if res.status_code == 404:
print('There is a problem with the request URL. Make sure that it is correct')
else:
print('There was a problem retrieving data: ', res.text)
else:
f = open('{}-{}-{}-to-{}.csv'.format(app_id, report_type, params['from'], params['to']), 'w', newline='', encoding="utf-8")
f.write(res.text)
f.close()
fileName = f.name
df = pd.read_csv(os.path.join(__location__, fileName));
df_columns = len(df.columns);
newdf = df[df.columns[0:10]]
newdf = newdf.fillna(0)
dfList = newdf.values.tolist()
try:
secretFile = 'google_client_secret.json'
scopes = ["https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
secret_file = (os.path.join(__location__, secretFile))
credentials = service_account.Credentials.from_service_account_file(secret_file, scopes=scopes)
service = discovery.build('sheets', 'v4', credentials=credentials)
spreadsheet_id = '1Uxxxxxxxxxxx'
range_name = 'Test!A2'
values = dfList
if not values:
data = { 'values': 'no-data-found' }
else:
data = { 'values': values }
#delete the temp csv file
if os.path.exists(os.path.join(__location__, fileName)):
os.remove(fileName)
else:
print("The file does not exist")
request = service.spreadsheets().values().update(spreadsheetId=spreadsheet_id, range=range_name, valueInputOption='USER_ENTERED', body=data)
response = request.execute()
except OSError as e:
print (e)
|
<br/>
I am wondering whether that's the problem of the csv file, as I am trying to create it in the cloud instead of the local machine. Or maybe it's the files that I created in the PythonAnywhere folder but I was trying to call in this file? (Ex. time_env file that needs to be imported into this file to run the dates) Any ideas why the scheduling doesn't work?