I'm writing an application in Flask, that consists of two files, app.py and tags.py. app.py imports tags.py, which in turn contains from eyed3 import load. Eyed3 is a Python module to extract mp3 tags from files. For some reason import of Eyed3 suppresses Flask's console output. Usually when starting Flask with python app.py returns:
* Running on http://127.0.0.1:17000/
When Eyed3 is imported, this line doesn't appear. It doesn't matter, whether it is import eyed3 or from eyed3 import load, or if import is in app.py or tags.py, or Flask has debug mode on/off. I even tried to run
import sys
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
just after import or before app.run(), no success. Why and how does mere import of a module hide console output? How can i restore it?
Edit: Order of import doesn't matter. Nothing happens, if i import Eyed3 before Flask, error still holds. Does it have to do with this line of code?
I'm willing to bet it has something to do with the logging manipulations that EyeD3 is doing in this file: https://bitbucket.org/nicfit/eyed3/src/97905ecfcd6c8f8df0349582b90258b154e583b5/src/eyed3/utils/log.py?at=default
Related
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__'))))
I have a config.yaml file that I want to import in my app.py.
This is my folder structure:
/root
__init__.py
app.py
config.yaml
App.py:
import logging
import config
from flask import Flask
app = Flask(__name__)
logger = logging.getLogger(__name__)
app.run(port=5001)
I have no problem importing other Python modules (.py) but when I try to import my YAML file, i get:
ImportError: No module named config
Does anyone know why this does not work? Do I need any dependencies to be able to import a YAML file like this?
Why do you expect to be able to import a YAML file? It isn't Python, and it isn't an extension module, and these are the only things that Python will import.
YAML (Yet Another Markup Language) is a data format, so you have to read the data with a suitable library: this answer might give you some clues.
You cannot import YAML files directly into Python as you suggest in your example - only *.py files. You can import the yaml Python module and parse your file, as shown in these SO answers:
How can I parse a YAML file in Python
Parsing a YAML file in Python, and accessing the data?
I am created an api for OpenERP using bottle and when i try to configure wsgi with apache.
While importing in apache server log it shows
ImportError: No module named api
I checked for current directory it prints cwd and the import file is in same directory still it shows error
Here i uploaded my code for wsgi
import os
os.chdir(os.path.dirname(__file__))
import bottle
print os.getcwd()
import api as application
application = bottle.defaut_app()
What is your sys.path?
It must include the directory of your .py file, not the file name itself. E.g.,
sys.path.append('/var/www/api')
# and then, to see its new value:
print sys.path.append
Also note how I printed the value of sys.path there; the way you did it in one of your comments (printing the return value from append) is incorrect.
I think i had a similar problem and I ended up doing this in my wsgi:
import sys
import os
import bottle
sys.path.append('%path to module%')
import %modulename%
application = bottle.default_app()
In your py you have to import:
from bottle import default_app
for this to work.
How do I explicitly tell Google App Engine (python) to import json from the python standard libraries?
Due to a poorly named file (that I can NOT change or rename at this time) I am having trouble importing json.
There is a json.py in the same directory as the file that I am working on.
When I try:
import json
it imports the file in the same directory.
Is there a way I can do something along the lines of:
from ../ import json
to import the Native JSON library?
EDIT:
I have now even tried renaming the offending file and replacing all uses of this file. But I'm still not able to import the json standard lib through GAE.
Attached is the error log:
File "/Users/admin/Blah/dataaccess.py", line 13, in <module>
from classes import ZenDesk
File "/Users/admin/Blah/classes/ZenDesk.py", line 10, in <module>
import json
File "/Users/admin/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 892, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named classes.json
Please Help google app engine should be looking for the standard library and not in the subdirectory classes
If you can't rename the module for some reason, you have to mess around with sys.path to put your standard lib in front of the current module. Make sure to fix it again after you do your import, though...
import sys
sys.path = [r'C:\Python27\Lib'] + sys.path
import json
sys.path = sys.path[1:]
Alternately, I'm pretty sure the imp module has functionality for this.
Let me be clear, though: it would be much better (as others have said) to just rename the module in the project directory (e.g. to my_json.py). You're not going to have to change that many dependencies.
Some Edits:
If you cannot find an actual path, absolute or otherwise, to the standard lib, you have two further options besides renaming the thing. You can move the file you're working on to a different level in your package (so that it can't see the local json.py). For instance, like this:
/package
__init__.py
your_file.py # "import json" should now hit the std lib.
# To import the local copy, use "from app import json"
app/
__init__.py
json.py
other_thing.py # contains "import json" to get the local copy
If you don't want to move your file, then you can fake it by adding an extra file that does nothing but import the real json module:
/package
__init__.py
json_importer.py # contains nothing but "import json"
app/
__init__.py
json.py
your_file.py # "from package.json_importer import json"
other_thing.py # contains "import json" to get the local copy
Try this perhaps?
import imp
json = imp.load_source('module.name', '/path/to/built-in/json/file.py')
Try this, although I'm not sure if it will work on GAE since its a PaaS platform:
import os
import sys
from distutils.sysconfig import get_python_lib
sys.path.insert(0,os.path.dirname(get_python_lib()))
import json
sys.path = sys.path[1:]
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