Skip to content Skip to sidebar Skip to footer

Importerror: No Module Named Selenium Cronjob Mac

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 ... seleni

Solution 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

Solution 2:

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>

Solution 3:

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 calllast):
  File "cli_multi_address_scraper.py", line 4, in<module>from selenium import webdriver
ModuleNotFoundError: Nomodule 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

Solution 4:

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.

Post a Comment for "Importerror: No Module Named Selenium Cronjob Mac"