I'm using the Bottle framework for a simple application that I'm working on atm. I have my bottle library located in the folder "lib" and I call the bottle framework from the lib folder by "import lib.bottle". This is my folder structure:
lib
- bottle.py
- bottledaemon.py
- __init__.py
view
- log-in.tpl
mybottleapp.py
This is my code:
#!/usr/bin/env python
import lib.bottle
from lib.bottle import route, template, debug, static_file, TEMPLATE_PATH, error, auth_basic, get, post, request, response, run, view, redirect, SimpleTemplate, HTTPError
from lib.bottledaemon import daemon_run
import os
import ConfigParser
#######################
# Application Logic #
#######################
# This line of code is not recognised:
app = bottle.default_app()
##################
# Page Routing #
##################
##### LOG-IN PAGE #####
#route('/')
#view('log-in')
def show_page_index():
outout = 0
# Pathfix for Daemon mode
TEMPLATE_PATH.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "view")))
debug(mode=True)
# Pass to the daemon
if __name__ == "__main__":
daemon_run()
So it throws this error at me:
"name app = bottle.default_app() not defined"
If I remove this line "app = bottle.default_app()" the app works fine BUT I realy want to have it in there for programming purposes.
So what am I doing wrong? Is it maybe cuz I run the app in daemon mode or maybe I don't call it right from the lib folder?
Btw I also can't import ConfigParser. This maybe has a diffirent cause but I can't use it.
I think all you need to do is change this:
import lib.bottle
to this
import lib.bottle as bottle
Note: in my setup all I need to do is this:
import bottle
So it throws this error at me: name app = bottle.default_app() not defined
Lies
Your error is actually
Traceback (most recent call last):
File ..., line ..., in ...
app = bottle.default_app()
NameError: name 'bottle' is not defined
Because you did not define bottle. You defined lib.bottle. Either use your new name
app = lib.bottle.default_app()
or rename it:
import lib.bottle as bottle
Related
I'm new to Google App Engine and having the trouble that the app doesn't find my module. I get the error line 5, in <module> import foo as bar ModuleNotFoundError: No module named 'foo'. I have the current file structure as seen below (following a great tutorial for Flask).
The problem is that routes.py cannot import foo.py.
app engine:/
app
static/css
templates
__init__.py
foo.py
routes.py
app.yaml
config.py
main.py
requirements.txt
source-context.json
Why is this? Are there special requirements for how files are structured on App Engine as this works locally?
Also, just to have things working I've tried having the code in the module foo in routes instead and the code works. But the code doesn't belong there and I want to structure it better but the app breaks when separating. In the end I would like to add directory "app engine":/app/libs (or else on recomendation) where I store my custom stuff.
EDIT (add code sample from routes.py)
from flask import render_template, flash, redirect, url_for
from app import app
from app.forms import LookupForm
import logging
import foo as bar
#app.route("/")
#app.route("/index")
def index():
return render_template("index.html")
I was able to reproduce the error you are experiencing. Here are my observations:
You are storing the foo module in a local folder called 'app' (a sub-directory of where you have your main.py file).
In order to reference the module in this situation, you would need to include the name of the sub-directory when doing the import.
Change the following line in your routes.py file:
import foo as bar
to:
import app.foo as bar
I have tested this solution and it worked for me. Please let me know if it helps.
Currently I am trying to a deploy my first FLASK application on PythonAnywhere.
Im not sure if this is the correct terminology but I have a folder as a module and there for I can't seem to find the correct way to deploy my application. I am not even sure where to start in resolving this issue. Any advice?
File and Folder Layout Snipped
my init.py code is:
import os
from flask import Flask
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='secret',
DATABASE=os.path.join(app.instance_path, 'LAMA.sqlite'),
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
# database
from . import db
db.init_app(app)
# authentication blueprint
from . import auth
app.register_blueprint(auth.bp)
# blog blueprint - the main index
# from . import blog
# app.register_blueprint(blog.bp)
# app.add_url_rule('/', endpoint='index')
# book blueprint
from . import book
app.register_blueprint(book.bp)
app.add_url_rule('/', endpoint='index')
return app
I have also followed the python debugging page where I have done the following:
>>> import LAMA
>>> print(LAMA)
<module 'LAMA' from '/home/ivanv257/LAMA_MAIN/LAMA/__init__.py'>
So at this stage in my WSGI configuration file I have:
import sys
path = '/home/ivanv257/LAMA_MAIN/LAMA/__init__.py'
if path not in sys.path:
sys.path.append(path)
from LAMA import app as application
I have also tried many other combinations such as
path = '/home/ivanv257/LAMA_MAIN/LAMA/'
from init import app as application
path = '/home/ivanv257/LAMA_MAIN/'
from init import app as application
path = '/home/ivanv257/LAMA_MAIN/'
from LAMA import app as application
my source code path is : /home/ivanv257/LAMA_MAIN/LAMA , although I have also tried different combinations such as /home/ivanv257/LAMA_MAIN/
ERROR DETAIL:
2018-12-08 10:05:32,028: Error running WSGI application
2018-12-08 10:05:32,030: ModuleNotFoundError: No module named 'LAMA'
2018-12-08 10:05:32,030: File "/var/www/ivanv257_pythonanywhere_com_wsgi.py", line 83, in <module>
2018-12-08 10:05:32,030: from LAMA import app as application # noqa
To solve my problem I changed the following (with some assistance) from lama import create_app:
import sys
path = '/home/ivanv257/LAMA_MAIN/LAMA'
if path not in sys.path:
sys.path.append(path)
from lama import create_app
application = create_app()
I also had to remove the from . to just imports
import db
db.init_app(app)
# authentication blueprint
import auth
app.register_blueprint(auth.bp)
You are close. To deploy your app, navigate to the webapps page on your user dashboard. If you have not done so already, click the "Add new a webapp" button and enter the desired name of the app. Then, on the same webapp page on the dashboard, scroll down to the "Code" section of the page. Click on the "source code" a href and add the absolute path (full path) to the lama_main directory storing your init.py file.
Next, click on "WSGI configuration file" link. In the WSGI configuration file for your app, set the correct path to the parent directory and import app from the init.py file:
import sys
# add your project directory to the sys.path
project_home = u'/home/your_user_name/lama_main'
if project_home not in sys.path:
sys.path = [project_home] + sys.path
# import flask app but need to call it "application" for WSGI to work
from init import app as application #note that the module being imported from must be the file with "app" defined.
Then, save the WSGI file and return to the webapp panel on your dashboard. Click the "Reload {your site name}" button. Now, you should be able to visit the site by clicking on the main link at the top of the page.
I have two files, app.py and database.py in the same directory.
Primarily I have the following code snippets:
app.py
import database
db = "demo_database"
print(database.show_database_information())
database.py
from app import db
database_username = "root"
database_password = "password"
def show_database_information():
information = {}
information["filename"] = db
information["username"] = database_username
information["password"] = database_password
return information
When I try to run app.py I got the following error:
Traceback (most recent call last):
File "K:\PyPrac\circular_call\app.py", line 1, in <module>
import database
File "K:\PyPrac\circular_call\database.py", line 1, in <module>
from app import db
File "K:\PyPrac\circular_call\app.py", line 3, in <module>
print(database.show_database_information())
AttributeError: module 'database' has no attribute 'show_database_information'
Then I updated app.py and included __main__ check like below:
app.py
import database
db = "demo_database"
if __name__ == '__main__':
print(database.show_database_information())
Now it runs smoothly without any error.
I have several questions,
What is name of the error occurred in first scenario? Need explanation.
Why it runs after including __main__ scope?
What is the better approach of doing operations like this?
What I can understand are as below's. Maybe someone more expert can elaborate !
Import error.
if __name__ == '__main__': This condition is used to check whether a python module is being run directly or being imported.
If a module is imported, then it's __name__ is the name of the module instead of main. So, in such situations it is better to call if __name__ == '__main__':
Man!! You are creating a circular moment. Let me tell how.
import database # from app.py
But from database.py you imported db from app. That is creating a circular moment.
On the other hand,
if __name__ == '__main__':
this is making you database.py as a name of the module instead of __main__ that's why it's working. Nothing magical :)
UPDATE: Placed from app import db this line inside the function show_database_information()
This is the HOTFIX for you.
I would like to write doctests for my pyramid web app, using the webtest module. I tried it like this:
from my_webapp import main
from webtest import TestApp
app = TestApp(main({}))
result = app.get('/')
This raises a KeyError (because some.url is not known) when my code reaches this line:
url = request.registry.settings['some.url']
The value of some.url is specified in the paster ini file of my application. Is there a simple way to use my development.ini when running my test code? I did not yet fully understand how/when the ini file is loaded during pyramid start up, so it's hard to figure out where to load it while testing.
main is invoked with the contents of your ini file. A simple way to load your app from an ini is:
from pyramid.paster import get_app
app = get_app('testing.ini#main')
test_app = TestApp(app)
This expects "testing.ini" to be in the current working directory, so you may need to tweak that. If you'd like it to be relative to a spot in your tree you can use:
import os.path
import some_module
here = os.path.dirname(some_module.__file__)
app = get_app(os.path.join(here, 'testing.ini'))
OK, I have the following directory structure (it's a django project):
-> project
--> app
and within the app folder, there is a scraper.py file which needs to reference a class defined within models.py
I'm trying to do the following:
import urllib2
import os
import sys
import time
import datetime
import re
import BeautifulSoup
sys.path.append('/home/userspace/Development/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
from project.app.models import ClassName
and this code just isn't working. I get an error of:
Traceback (most recent call last):
File "scraper.py", line 14, in
from project.app.models import ClassName
ImportError: No module named project.app.models
This code above used to work, but broke somewhere along the line and I'm extremely confused as to why I'm having problems. On SnowLeopard using python2.5.
import sys
sys.path.append ('/path/to/the/project')
from django.core.management import setup_environ
import settings
setup_environ(settings)
from app.models import MyModel
Whoa whoa whoa. You should never ever have to put your project name in any of your app code. You should be able to reuse app code across multiple projects with no changes. Pinax does this really well and I highly recommend checking it out for a lot of django best practices.
The worst thing you could do here is to hard code your absolute path into your app or settings. You shouldn't do this because it will break during deployment unless you do some import local_settings hacking.
If you have to access the project root directory, try what pinax has in settings.py...
import os.path
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
The thing is that it looks like you are trying to access the models module within the same app and this is waaay easier.
To import models.py inside scraper.py in the same directory just use import models or import models as app_models if you already have something named models in scraper.py (django.db.models for instance). Are you familiar with Python module conventions?
However, the best way is probably to stick with the django idiom, from ... import ... statement:
from app import models
If this doesn't work automatically, then something is wrong in your settings.py.
You don't indicate if project is located in /home/userspace/Development/. I'll assume that it is.
Make sure there's an (empty by default) file named __init__.py in project and another one in app.
EDIT: Next thing to try: Fire up the Python command line in the script's directory and try the following:
import project
import project.app as app
import project.app.models as models
models.__dict__.keys()
Do they all work? If so, what is the last line's output? If not, which dies first?