Importing Python libraries in Openshift cron jobs - python

I have a script in .openshift/cron/daily that looks like this
#!/usr/bin/python
import sys
import os
sys.path.append(os.environ['OPENSHIFT_REPO_DIR'])
import EmilyBlogModel
EmilyBlogModel.Poll()
EmilyBlogModel.py is in $OPENSHIFT_REPO_DIR
However, when the cron job runs, I get an ImportError
No module named EmilyBlogModel
Why isn't this working?

Have you tried printing the system path before the import statement? OPENSHIFT_REPO_DIR might be pointing to the wrong path.

Related

Python script doesn't work when i run it with "python3 absolute path" but work when i navigate on the folder then run

i have to implement a python script in a wordpress project.
so first i tried to run it from command line and it work correctly when i open my bash from the folder and run it
but when i tried to run it by drag and drop, it return me the following error
for my project, i need to make it work when i use the command "python3 absolute/path"
can someone help me with it ?
i work on ubuntu if it may help
edit : here is what i tried
here the code previously here on the main.py :
#!/usr/bin/env python3
import json
from photovoltaique.roiPhoto import *
from bornes_rentabilite.pertinence import *
from bornes_rentabilite.calcul_roi_v2 import *
import sys
import os
sys.path.append(os.getcwd()+"/bornes_utiles")
sys.path.append(os.getcwd()+"/bornes_rentabilite")
sys.path.append(os.getcwd()+"/photovoltaique")
and what i had add :
sys.path.insert(0, "/bornes_utiles")
sys.path.insert(0, "/bornes_rentabilite")
sys.path.insert(0, "/bornes_photovoltaique")
and the architecture of the projet :
Architecture
you need to tell it where to look for your modules ... I would think this would work
PYTHONPATH=/home/xxxx/code python3 /home/xxx/code/script.py
you could also at the top of your main.py do it there as well
# at top of main.py
import sys, os
sys.path.insert(0,os.path.dirname(__file__))
import other_modules

How to import one module to another in Django/Python?

I'm just getting started with Python and Django. So within my Django application, I created a new module and within that, I want to import some variable defined in the parent module. However, I am getting certain errors while I tried various combinations.
Below is how my directory structure looks like
Now in my kafka_producer.py I am trying to import constants.py.
kafka_producer.py:
from confluent_kafka import Producer
import sys
import logging
import json
from my_app.constants import KAFKA_BROKER_URL
logging.basicConfig()
#Assign Configuration
conf = {'bootstrap.servers': KAFKA_BROKER_URL}
print(conf)
However, I am getting no module found error. What is the correct way of importing modules in Python?
It should work if you write:
from ..constants import KAFKA_BROKER_URL
Through this way, you leave the kafka directory and you are in your my_app directory.
Can you please paste full trace of error, as in :
ModuleNotFoundError: No module named --name
I want to know what it is saying in --name.
One thing you can try is adding path of your app to the environment variable PATH.
Try adding below lines in your kafka_producer.py
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath('__file__'))))

Importing Module python

I have added:
export PYTHONPATH="${PYTHONPATH}:/home/twittercap/alchemyapi"
to my ~/.profile file (ubuntu server environment) and it shows when I run
import sys
print sys.path
but it won't let me import the module using
from alchemyapi import AlchemyAPI
(which I can when running from within the directory.
Any help is appreciated.
Update:
I can now import alchemyapi but import alchemyapi.AlchemyAPI returns ImportError: No module named AlchemyAPI (but there is!)
Resolved: Git cloned again and didn't rename - either the rename messed it up, or else the files corrupted the first time - thanks for your suggestions though!

Python Import Module

Recently started a new Python project.
I am resolving a import module error where I am trying to import modules from the same directory.
I was following the solutions here but my situation is slightly different and as a result my script cannot run.
My project directory is as follows:
dir-parent
->dir-child-1
->dir-child-2
->dir-child-3
->__init__.py (to let python now that I can import modules from here)
->module1
->module2
->module3
->module4
->main.py
In my main.py script I am importing these module in the same directory as follows:
from dir-parent.module1 import class1
When I run the script using this method it throws a import error saying that there is no module named dir-parent.module1 (which is wrong because it exists).
I then change the import statement to:
from module1 import class1
and this seemed to resolve the error, however, the code I am working on has been in use for over 2.5 years and it has always imported modules via this method, plus in the code it refers to the dir-parent directory.
I was just wondering if there is something I am missing or need to do to resolve this without changing these import statements and legacy code?
EDIT: I am using PyCharm and am running off PyCharm
If you want to keep the code unchanged, I think you will have to add dir-parent to PYTHONPATH. For exemple, add the following on top of your main.py :
import os, sys
parent_dir = os.path.abspath(os.path.dirname(__file__)) # get parent_dir path
sys.path.append(parent_dir)
Python's import and pathing are a pain. This is what I do for modules that have a main. I don't know if pythonic at all.
# Add the parent directory to the path
CURRENTDIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if CURRENTDIR not in sys.path:
sys.path.append(CURRENTDIR)

Python module import error (Works fine in linux but fails in FreeBSD)

We have an application which is currently working on a linux system. Now I am trying to port it to FreeBSD. We are running the application using twistd
/usr/bin/twistd -y $TACFILE --rundir $RUNDIR --logfile=/dev/null --pidfile=$PIDFILE
My tacfile is a python file which starts with these lines
#!/opt/python/bin/python
import os
from twisted.application import internet, service
from twisted.internet import reactor
from twisted.enterprise import adbapi
from twisted.plugin import getPlugins
from twisted.python import log
import labris.flyng.iflyng as iflyng
import labris.flyng.config as config
import labris.flyng.plugins as pplugins
import labris.flyng.protocols as flyng_protocols
But I get an error in this line:
import labris.flyng.iflyng as iflyng
The error is:
exceptions.ImportError: No module named labris.flyng.iflyng
Failed to load application: No module named labris.flyng.iflyng
But as you can understand twisted imports dont cause an error whereas labris imports fail. This application runs without any errors in linux.
And if it has something to do with it /opt/python/bin/python is a symbolic link pointing to /usr/local/bin/python2.6
Both twisted and labris directories are under the path
/usr/local/lib/python2.6/site-packages
Their permissions are correct and each of them has the same permissions.
The output of ls /usr/local/lib/python2.6/site-packages/labris/flyng is
__init__.py config.py db iflyng.py parsers plugins protocols.py
So I dont think there is an error with the module's path. I don't know what might be the cause of this error. Any clues, pointers is most welcome.
You might also try to import the module from a simple python script to see if it is installed in the right place

Categories