Cron wont import pandas module to execute python script. ```ImportError: No module named pandas```

懵懂的女人 提交于 2021-01-27 18:24:07

问题


I am using xubuntu 18.01

I have a python program that that scrapes weather data and saves the files as a csv. It works perfectly running the command weatherdata in terminal after i gave it permission using chmod +x weatherdata.

I would like this to run every 2 weeks using cron. But nothing happens after i set it up.

I am using the NANO cron editor

I have set the cronjob up trying PATH variables, SHELL=/bin/bash/. P Putting the /bin/bash in front of the command... all to no avail....

# crontab -e
SHELL=/bin/bash
MAILTO= mygmail@gmail.com (removed my actual email for privacy)
PATH=/home/luke/bin/
# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command


34 * * * * /bin/bash /home/luke/bin/weatherdata

I expected the weatherdata python file to be executed every hour at 34 minutes past the hour.

Nothing happens, I don't get an email or anything.


Edit:

I changed to top to

SHELL=/bin/bash
#MAILTO=mygmail@gmail.com (commented it out)
PATH=/home/luke/bin/:/usr/sbin:/usr/bin:/sbin:/bin

what i get after running: 15 * * * * /bin/bash /home/luke/bin/weatherdata > /tmp/weatherlog 2>&1 as suggested in the comments.

Traceback (most recent call last):
  File "/home/luke/Documents/Duka/Master_River_Levels_LOOP.py", line 9, in <module>
    import pandas as pd
ImportError: No module named pandas
Traceback (most recent call last):
  File "/home/luke/Documents/Duka/Temp_Mean_Weather_loop.py", line 9, in <module>
    import pandas as pd
ImportError: No module named pandas

I need to import a few modules for the python script to run, the first one being panadas.


回答1:


The crontab -e job looks fine.

Why not just use:

34 * * * * /home/luke/bin/weatherdata

Or else set a sh file ... eg. myfile.sh

#!/usr/local/env bash
/home/luke/bin/weatherdata

Then crontab -e reads

34 * * * * /home/luke/myfile.sh

Cron works a dream unless your system admin is blocking it.




回答2:


As @thatotherguy suggested it was my path that was the problem.

1) I ran echo $PATH in terminal and got back:

/home/luke/anaconda3/bin:/home/luke/anaconda3/condabin:/home/luke/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

2) I copy and pasted the above path to replace: PATH=/home/luke/bin/:/usr/sbin:/usr/bin:/sbin:/bin with

PATH=/home/luke/anaconda3/bin:/home/luke/anaconda3/condabin:/home/luke/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin 

3) I changed PATH=/ to PATH=$PATH:/

Now the cronjob executes perfectly.



来源:https://stackoverflow.com/questions/54564187/cron-wont-import-pandas-module-to-execute-python-script-importerror-no-modu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!