Cannot Import Python MySQL module when running a script using crontab

后端 未结 5 1777
悲哀的现实
悲哀的现实 2020-12-30 07:34

I am using crontab to run a python script that requires the module MySQLdb. When I run this script from the command line everything works fine. However, trying to run it usi

5条回答
  •  别那么骄傲
    2020-12-30 08:05

    It may be that you're using a different Python executable. On the shell, enter which python to find out where the Python executable is located. Let's say this returns something other than /usr/bin/python, say /home/myuser/bin/python, then in the first line of your script, you would write:

    #!/home/myuser/bin/python
    

    It may also be that your shell has environment variable called PYTHONPATH. If that's the case and you find where it's importing the library from, then this is how you would add the path to find the library in the first line of your script, before the import of "MySQLdb":

    import sys; sys.path.append('/path/to/MySQLdb-lib/')
    

提交回复
热议问题