crontab python script error with dependencies - python

I have an scheduled crontab to run every minute that calls a shell script that runs the python script. My problem is that I get an ModuleNotFoundError. I'm a newbie in terms of cronjobs and Linux
crontab
* * * * * sh /home/pablo_racana/queries/test.sh >> out.txt 2>&1
sh
#!/bin/sh
cd "$(dirname "$0")";
CWD="$(pwd)"
echo $CWD
PYTHONPATH=/home/pablo_racana/.local/lib/python3.7/site-packages/
HOME=/home/pablo_racana
python3 test_cron.py
python script
import datetime
import sys
import pyodbc
import json
import struct
import adal
import time
import pandas as pd
import numpy as np
import os
For each iteration I receive the following error
Traceback (most recent call last):
File "test_cron.py", line 6, in <module>
import adal
ModuleNotFoundError: No module named 'adal'
I saw in a different answer that I needed to add the PYTHONPATH to where my package is installed, but in my case, I have packages instaled in different folders
import pyodbc
print(pyodbc)
/home/pablo_racana/.local/lib/python3.7/site-packages/
import adal
print(adal)
/opt/conda/lib/python3.7/site-packages/

solved.
I added the following script with the filepaths
import sys
sys.path.append(r'/home/pablo_racana/.local/lib/python3.7/site-packages/')
sys.path.append(r'/opt/conda/lib/python3.7/site-packages/')

Related

ModuleNotFoundError: No module named 'data_receiver'

when I go to run my program, I keep receiving the error message below:
pi#navio:~/cloudapp-raspi $ sudo python3 app.py
Traceback (most recent call last):
File "app.py", line 7, in <module>
from data_receiver import DataReceiver
ModuleNotFoundError: No module named 'data_receiver'
however, when I go in the app.py file (see below), I clearly have it being imported.
import logging, time, argparse, configparser, sys
import socket, os, signal, psutil, data_receiver
from subprocess import Popen
from drone import Drone
from connection_watchdog import ConnectionWatchdog
from data_receiver import DataReceiver
from utils import Utils
data_receiver is not a standard python package. If you are installing it from a third party source using a package manager (like pip), be sure to install it globally, i.e., something like sudo -H pip install <package_name>.
Otherwise, if you have a data_receiver.py in your system, make sure to put it in the same directory as your app.py.

Can anyone help in fixing this constant "ModuleNotFoundError" on Google Colaboratory?

here is my code to import some python libraries:
from radmc3dPy import *
from radmc3dPy.image import *
from radmc3dPy.analyze import *
from radmc3dPy.natconst import *
import matplotlib.pyplot as plt
import numpy as np
import os
from astropy.io import fits
import matplotlib.gridspec as gridspec
On running the cell, I receive the following error even after factory resetting the runtime:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-225d47afe30d> in <module>()
----> 1 from radmc3dPy import *
2 from radmc3dPy.image import *
3 from radmc3dPy.analyze import *
4 from radmc3dPy.natconst import *
5 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'radmc3dPy'
you need to download and install the library before you can use it. I saw that there is a github for your package here: https://github.com/dullemond/radmc3d-2.0
in colab, you can install it with:
!git clone https://github.com/dullemond/radmc3d-2.0.git
%cd ./radmc3d-2.0/python/radmc3dPy/
!python setup.py install
%cd ~/../content
! and % are required.
The comand clones the desired directory from git, changes into the python directory, runs the setup of your package and changes your working directory back. Directly running the setup.py with the full path, did not work, hence changing the directory.

Python3 - ImportError: No module named

I have 2 folders:
my_python
code.py
MyCode
TestEntry.py
When I run the following commands:
cd /data/my_python
python3 code.py
The above works.
However, if I in my home folder and then run this:
python3 /data/my_python/code.py
I get the following error:
Traceback (most recent call last):
File "/data/my_python/code.py", line 4, in <module>
from TestEntry import TestEntry
ImportError: No module named 'TestEntry'
Here is the code:
import sys
import os
sys.path.append(os.path.abspath('../MyCode'))
from TestEntry import TestEntry
TestEntry().start(507,"My Param1","/param2",'.xyz',509)
Can you help me how to fix this?
You are adding a relative path to sys with your line sys.path.append(os.path.abspath('../MyCode')). Instead, you need to import relative to that file you are calling. Try this:
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from TestEntry import TestEntry
TestEntry().start(507, "My Param1", "/param2", '.xyz', 509)
That happens because, as #mkrieger1 mentioned, your sys.path gets messed up. I have a previous answer here which explains how to set it. By sys.path getting messed up, I mean that python will look in the dir that you are running from, not the dir that the script you are running is in. Here is the recommended method:
import sys, os
sys.path.append(os.path.abspath(os.path.join('..', 'MyCode')))
... (your code)
or
import sys, os
sys.path.append(os.path.abspath(os.path.join(__file__, '..', 'MyCode')))
... (your code)
This way python will look in the dir of the file you are running as well.

ModuleNotFoundError: No module named 'googlemaps' - though googlemaps installed

I am running an app.py which imports googlemaps as
import googlemaps
But when I do a pip list, it lists googlemaps as
googlemaps 3.0.2
When I run the app,
Traceback (most recent call last):
File "app.py", line 40, in <module>
import googlemaps
ModuleNotFoundError: No module named 'googlemaps'
I am on Windows, Python 3.6.0
In order to be sure. that pip and your script use the same python version I suggest to call following commands from the same terminal window.
python3 -m pip freeze | grep -i googlemaps
python3 -c "import sys ; print(sys.exewutable)"
python3 -c "import sys ; print("\n".join(sys.path))"
Try even following line to prove that googlemaps is installed if not being called from your app
python3 -c "import googlemaps"
and
python3 app.py
If you do not call your app directly with python, then tell us how you start the app. This different way of calling might be why it is not pointing to the same python version / virtualenv than the one where you installed googlemaps
If you do not know how the app is exactly started (e.g. started by a web server or similar), then I suggest you add following lines to app.py
before the import line that fails.
import os, sys
# the next two l
with open(os.expanduser("~/debug.txt", "w") as fout:
fout.write("EXE: %s\n" % sys.executable)
fout.write("PATH:\n" + ("\n".join(sys.path)))
This should create a file named debug.txt,
that you can inspect

Django- Stand alone script is not in Django environment even with $DJANGO_SETTINGS_MODULE set

I am trying to run a script in my django project as a stand alone. I am using Django 1.6, this is a development server so no uwsgi is used, and I am running in a python virtual server. The virtual environment seems good to me because I can run python runserver 0.0.0.0:9000 with no issues.
Here is my environment set up:
(bot)one#chat-dash /home/git/bot_server/bot_server/bot_server $ export DJANGO_SETTINGS_MODULE=bot_server.settings.local
(bot)one#chat-dash /home/git/bot_server/bot_server/bot_server $ echo $DJANGO_SETTINGS_MODULE
bot_server.settings.local
Here is the my script:
(bot)one#chat-dash /home/git/bot_server/bot_server/bot_server/bot_data $ cat req.py
import sys
import os
import datetime
import base64
import json
from generators import thread_generator
from do_request import do_request
os.environ['DJANGO_SETTINGS_MODULE'] = "bot_server.settings.local"
from .models import User, ThreadVault
Here is the output from running:
(bot)one#chat-dash /home/git/bot_server/bot_server/bot_server/bot_data $ python req.py
Traceback (most recent call last):
File "req.py", line 10, in <module>
from bot_data.models import User, ThreadVault
ImportError: No module named bot_data.models
(bot)one#chat-dash /home/git/bot_server/bot_server/bot_server/bot_data $
I found that the script will run correctly if outside/parent of bot_data. I will need to figure out why it the path of bot_data/req.py takes it outside of the Django enviroment.

Categories

Resources