2

I am trying to automate a python script to run every minute but can't seem to figure out why I get the error:

ImportError: No module named selenium

although:

pip freeze
...
selenium==3.4.2
...

and script runs in command line.

here's what I am doing:

* * * * * cd /Users/Saleh/Desktop/MMM && python READY.py

Error:

crontab: installing new crontab You have new mail in /var/mail/Saleh Mohameds-Air:~ Saleh$ mail Mail version 8.1 6/6/93. Type ? for help. "/var/mail/Saleh": 1 message 1 new

N 1 Saleh@Mohameds-Air.l Sat May 20 13:31 22/816 "Cron cd /Users/Saleh/Desktop/MMM && python READY.py X-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: X-Cron-Env: Date: Sat, 20 May 2017 13:31:01 +0300 (EEST)

Traceback (most recent call last): File "READY.py", line 12, in from selenium import webdriver ImportError: No module named selenium

Path of python script is as below:

Users/Saleh/Desktop/MMM/READY.py

pls help, THANK YOU!

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
mosaleh95
  • 21
  • 4

4 Answers4

1

You have to have the path to to your selenium driver specified when running it as a cron. for example

export PATH=path/to/your/selenium/driver/folder:$PATH && cd /Users/Saleh/Desktop/MMM && python READY.py
pseudoanime
  • 1,547
  • 12
  • 19
1

I am adding this to answer my own question, posed in response to the question above as well as, I believe, the original question itself. None of the prior answers directly fixes the problem, if my experience is typical.

My answer is based on two other questions on SO, as well as an article on another site. Those other questions are 57101742 and 54564187

The solution is now working for me.

Steps:

  • in a terminal type:

    echo PATH

  • copy the full text that you get back.

  • open your crontable with

    crontab -e

  • paste the text into the first line the crontab so that it will be executed first.

  • edit the text so that it starts with PATH=$PATH:/Users/<USERNAME>

My PATH line reads PATH=$PATH:/Users/ian/opt/anaconda3/bin:/Users/ian/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

Save the file and when next your crob job is executed it should run.

NOTE: In one article that I read it suggested that you also need a bash script to run to activate the Anaconda / Miniconda environment. I have not found this to be the case and my Python script is being executed according to the cron scheduler.

Watty62
  • 602
  • 1
  • 5
  • 21
0

I had the same issue on Ubuntu 18.04.

For me the fix was make a shell script that calls the python script and in the shell script set the following at the top of the shell script:

HOME=<your_home_dir>
PYTHONPATH=<path_to_dist_packages>
Timothy C. Quinn
  • 3,739
  • 1
  • 35
  • 47
0

I have the same issue as @Mosaleh95..

I have created a python programme which runs successfully (straight from Pycharm ) or from the CLI.

When I set up a cron job on Mac OSX.

When cron attempts to execute it, the following error is raised

Traceback (most recent call last):
  File "cli_multi_address_scraper.py", line 4, in <module>
    from selenium import webdriver
ModuleNotFoundError: No module named 'selenium' 

yet if I run

Pip Freeze 

Selenium 3.141.0

I've looked at both answers above.

I am unsure of the similarities / differences with Ubuntu and Mac OSX (10.15.6) as suggested by Timothy Quinn.

Also, if I read @pseudoanime's answer that would appear to point to a missing web driver (not that Selenium self is missing).

As I say, I can run the programme from Pycharm, and from the CLI (using the same command as the cron job - including to cd to the correct directory).

I suspect that the question we should be asking is "How can I tell what environment the cron job is running under, so that I can run a script with the necessary libraries? And how can I change that environment? " Why the environment would would differ from the CLI execution, I don't know.

Thanks

Watty62
  • 602
  • 1
  • 5
  • 21
  • Apologies - you're right - I'll amend my response to refer to Mosaleh95. The question, I am asking, and I suspect what Mosaleh95 was actually trying to find and answer to is "How can I tell what environment the cron job is running under, so that I can run a script with the necessary libraries? And how can I change that environment? " I'll amend my post above. Thanks. – Watty62 Oct 05 '20 at 15:15
  • I'm not a python expect by any means. What I was trying to say was that your post read like a Question, not like an Answer. I've read your amended post, and I'm still a little unsure :) – Scratte Oct 05 '20 at 15:37