Forums

How can I activate my virtualenvs in my script for task

Let me ask a rudimentary question.

I want to back up my database using dropbox.

I installed dropbox into my virtualenvs using pip

and I confirmed dropbox as below with using pip list

Package            Version
------------------ ---------
...
dropbox            11.10.0
...

I placed my script for backup as below

/home/myusername/backuptask/backup_db.py

I tried running my script ,but I can't import dropbox, got error

ModuleNotFoundError: No module named 'dropbox'

How can I activate my virtualenvs and use dropbox in my script?

If you have a virtualenv called "myenv", and you want to use it in a scheduled task, just schedule a command that activates it and runs the script. For example, if the script is called myscript, you would schedule

workon myenv && python myscript

The same applies to always-on tasks.

Thank you very much, I could run myscript and backup my db.

Excellent!