I'm creating a Flask application, and attempting to load configuration from a file. Not too hard right? I'm loading it like this:
from flask import Flask
app = Flask(__name__)
app.config.from_pyfile('../config.py')
The config file looks something like this:
# App settings
DEBUG = True
TESTING = True
The issue is, it returns an error...
[root#helios gdn]# env/bin/python run.py
Traceback (most recent call last):
File "run.py", line 3, in <module>
from gdn import app
File "/var/www/gdn/gdn/__init__.py", line 5, in <module>
app.config.from_pyfile('../config.py')
File "/var/www/gdn/env/lib/python2.6/site-packages/flask/config.py", line 129, in from_pyfile
exec(compile(config_file.read(), filename, 'exec'), d.__dict__)
File "/var/www/gdn/gdn/../config.py", line 1
DEBUG = True
^
That is strange... So I tried replacing the contents, even with the lines print 'foo' / print 'bar' and it returns the same error, referencing the end of the line. Empty files work, and single lines work, but multiple lines? Nope. Each line is ended with a single \n, just like the rest of my Python files. I cannot find what is causing the issue.
I'm running Python 2.6.6 on a CentOS VM.
Make sure your config.py file is present just inside the project root and you can call that config file like this
app.config.from_object('config')
Hope it helps !
Related
I'm using the pgdumplib lib. Unfortunately there is an error, when I'm trying to open the file. The file is in the same folder as the python script. I'm using Python 3.7
Code:
import pgdumplib
dump = pgdumplib.load('test.dump')
print('Database: {}'.format(dump.toc.dbname))
print('Archive Timestamp: {}'.format(dump.toc.timestamp))
print('Server Version: {}'.format(dump.toc.server_version))
print('Dump Version: {}'.format(dump.toc.dump_version))
for line in dump.table_data('public', 'pgbench_accounts'):
print(line)
Error:
Traceback (most recent call last):
File "C:/Users/user/data/test.py", line 3, in <module>
dump = pgdumplib.load('test.dump')
File "C:\Users\user\venv\data\lib\site-packages\pgdumplib\__init__.py", line 24, in load
return dump.Dump(converter=converter).load(filepath)
File "C:\Users\user\venv\data\lib\site-packages\pgdumplib\dump.py", line 228, in load
raise ValueError('Path {!r} does not exist'.format(path))
ValueError: Path 'test.dump' does not exist
If you are running your code from C:/Users/user/700Joach/project/ and you have the following line in your script:
dump = pgdumplib.load('test.dump')
Then, python would look for the following path to open test.dump:
C:/Users/user/700Joach/project/test.dump
Namely, this part: load('test.dump') internally is forging a relative path to test.dump.
You can do several things to resolve the issue. Either move test.dump to the directory from which you are executing your code. Or, provide an absolute path to your test.dump as follows:
dump = pgdumplib.load('C:/Users/user/700Joach/project/test.dump')
I have Asterisk 13.20 set up and running fine on Ubuntu 16.04,calls are going well through a Softphone(Zoiper), I came across a Library in Python "Pycall" which lets you make calls through a Python Script.So I tried testing a snippet from the site Pycall
The code that I used:
from pycall import CallFile, Call, Application
call = Call('SIP/flowroute/18882223333')
action = Application('Playback', 'hello-world')
c = CallFile(call, action)
c.spool()
Just made changes in the extension,wherein I used one from my server
However I am getting the following error:
Traceback (most recent call last):
File "test.py", line 10, in <module>
c.spool()
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 131, in spool
self.writefile()
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 119, in writefile
f.write(self.contents)
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 114, in contents
return '\n'.join(self.buildfile())
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 96, in buildfile
raise ValidationError
pycall.errors.ValidationError
Tried searching online for the solution but coudnt find one,What am I doing wrong?
This question not related to PBX, it is just incorrect use of LIB.
General instruction how to solve ANY issue with availible source code.
You have error
File "/usr/local/lib/python2.7/dist-packages/pycall/callfile.py", line 96, in buildfile
Go that file and line, check code, add more debug if needed, fix your app.
Run as asterisk user.
sudo su asterisk -s /bin/bash
python
from pycall import CallFile, Call, Application
call = Call('SIP/flowroute/18882223333')
action = Application('Playback', 'hello-world')
c = CallFile(call, action)
c.spool()
I would like to note that the following error only occurs when ran through a celery worker.
with the following command in the terminal:
celery -A MarketPlaceTasks worker --loglevel=info
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/celery/app/trace.py", line 218, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/celery/app/trace.py", line 398, in __protected_call__
return self.run(*args, **kwargs)
File "/home/nick/mpcrawler2/MarketPlaceTasks.py", line 65, in get_item_data
logger, request, run_data, store_config, app_config = setup_task(payload)
File "/home/nick/mpcrawler2/MarketPlaceTasks.py", line 33, in setup_task
store_config = ConfigReader.read_store_config(request.Store)
File "/home/nick/mpcrawler2/shared/ConfigReader.py", line 22, in read_store_config
from singletons.StoreData import StoreData
File "/home/nick/mpcrawler2/singletons/StoreData.py", line 3, in <module>
from models.StoreConfig import StoreConfig
File "/home/nick/mpcrawler2/models/StoreConfig.py", line 3, in <module>
from enums.MpStores import MpStore
ImportError: No module named enums.MpStores
I have all my enums in a separate module. The module looks like this, and is located inside the same directory as the project:
Whenever I run the project via pycharm or terminal everything seems to work as intended.
The worker's starting point looks like this:
from celery import Celery
app = Celery('tasks', broker='*some ampq address here*')
The __init__.py file is empty. The enum files look like this:
from enum import Enum
# noinspection SpellCheckingInspection
class MpStore(Enum):
somevalue = 1
someothervalue = 2
etc = 3
As I'm using Python 2.7 I'm using enum34 that was installed using pip.
Please let me know if there's anything else I should provide in the question.
Well it seems like some sort of work around but following the advice in this answer:
How to accomplish relative import in python
I moved most of the project inside a "main" module containing all of them. and then i was able to:
instead of from enums.MpStore import MpStore
I now use from stuff.enums.MpStore import MpStore "stuff" being the new module name.
I would love to hear of a better way though...
I have a Django app running on heroku.
The app keeps crashing with the following stacktrace:
Traceback (most recent call last):
File "/app/.heroku/python/bin/honcho", line 9, in <module>
load_entry_point('honcho==0.4.2', 'console_scripts', 'honcho')()
File "/app/.heroku/python/lib/python2.7/site-packages/honcho/command.py", line 292, in main
app.parse()
File "/app/.heroku/python/lib/python2.7/site-packages/honcho/command.py", line 129, in parse
options.func(self, options)
File "/app/.heroku/python/lib/python2.7/site-packages/honcho/command.py", line 190, in start
sys.exit(process_manager.loop())
File "/app/.heroku/python/lib/python2.7/site-packages/honcho/process.py", line 114, in loop
File "/app/.heroku/python/lib/python2.7/site-packages/honcho/printer.py", line 22, in write
Process exited with status 1
From Honcho's printer.py source code it looks like someting is going wrong while honcho is trying to write to the console.
My app is pulling data from web pages so I may print some text with accented characters. Is that related ?
Currently i'm printing things like this:
logger.info('Saved article' + str(title) + '.')
I may also dump some json from time to time:
logger.debug('Article: %s' % json.dumps(article_datas, indent=4))
It is working fine on my development machine but not on Heroku.
Do I need to log things differently ? Why is this not working on Heroku ?
I finally found that my Heroku app needed the PYTHONIOENCODING evironment variable to be set to utf-8.
heroku config:set PYTHONIOENCODING=utf-8
See this question for details about stdout encoding.
I try to create a configuration file, where I can store constants.
Whenever I try with ConfigParser, I get an error
Traceback (most recent call last):
File "/home/baun/google_appengine/google/appengine/ext/webapp /__init__.py", line 511, in __call__
handler.get(*groups)
File "/home/baun/workspace/octopuscloud/s3/S3.py", line 138, in get
test = parser.get('bucket', 'bucketname')
File "/usr/lib/python2.5/ConfigParser.py", line 511, in get
raise NoSectionError(section)
NoSectionError: No section: 'bucket'
simple.cfg:
[bucket]
bucketname: 'octopus_storage'
s3.py:
...
from ConfigParser import SafeConfigParser
parser = SafeConfigParser()
parser.read('simple.cfg')
...
# Get values from the config file
test = parser.get('bucket', 'bucketname')
...
How can I fix this?
===============================
The problem is fixed. The code was correct, but simple.cfg was in the wrong directory.
[bucket]
bucketname= octopus_storage