Forums

scheduled task is not using my virtualenv, despite me specifying it.

I have daily_scrape.py that I want to run as a scheduled task. I built the software in a virtualenv. Therefore, I schedule the task as:

/home/straits/.virtualenvs/reddit/bin/python /home/straits/reddit/daily_scrape.py

But the routine is failing because an import praw statement fails, despite praw existing in my reddit virtualenv.

$ cd ~
09:35 ~ $ **/home/straits/.virtualenvs/reddit/bin/python /home/straits/reddit/daily_scrape.py**
scraping fresh titles
Traceback (most recent call last):
  File "/home/straits/reddit/daily_scrape.py", line 80, in <module>
    scrape_reddit()
  File "/home/straits/reddit/daily_scrape.py", line 59, in scrape_reddit
    scrape_df = scrape_fresh_titles(limit=300)
  File "/home/straits/reddit/daily_scrape.py", line 34, in scrape_fresh_titles
    reddit = praw.Reddit(client_id='Rcr4_FVKuWXX5Q',
**NameError: name 'praw' is not defined**

But if I use that same python shell, I can import praw fine:

09:35 ~ $ **/home/straits/.virtualenvs/reddit/bin/python** 
Python 3.6.0 (default, Jan 13 2017, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
**>>> import praw**
>>> exit()
09:36 ~ $

the daily_scrape.py file doesn't have the 'shebang' thing to specify any different shell.

Can't figure out what I'm doing wrong.

It also doesn't work from inside a virtualenv:

09:47 ~/reddit $ pwd
/home/straits/reddit
09:54 ~/reddit $ workon reddit
(reddit) 09:54 ~/reddit $ python daily_scrape.py 
scraping fresh titles
Traceback (most recent call last):
  File "daily_scrape.py", line 80, in <module>
    scrape_reddit()
  File "daily_scrape.py", line 59, in scrape_reddit
    scrape_df = scrape_fresh_titles(limit=300)
  File "daily_scrape.py", line 34, in scrape_fresh_titles
    reddit = praw.Reddit(client_id='Rcr4_FVKuWXX5Q',
NameError: name 'praw' is not defined
(reddit) 09:54 ~/reddit $ which python
/home/straits/.virtualenvs/reddit/bin/python
(reddit) 09:55 ~/reddit $ python
Python 3.6.0 (default, Jan 13 2017, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import praw
>>> print("works inside the shell?")
works inside the shell?
>>> exit()
(reddit) 09:55 ~/reddit $

And it works fine calling import praw from my python notebook whose kernel is set to the reddit virtualenv.

hmm- from what you said your virtualenv sounds very messed up.

Perhaps during the creation of it, something messed up / got cut off and therefore it's unusable?

I would remove the virtualenv and create a new one.

Sorry -- I was being stupid.

The error is NameError: name 'praw' is not defined -- not that it cannot find the module. It's that I forgot to import praw in the file....

okay- good to hear that you figured it out!