So, I am going over "Learn Python The Hard Way" and have an issue with Chapter 50 "Building my first website".
jharvard#appliance (~/Dropbox/Python/gothonweb): ls -R
bin docs gothonweb templates tests
./bin:
app.py
./docs:
./gothonweb:
__init__.py
./templates:
./tests:
__init__.py
Trying to run app.py file with command: $python bin/app.py It supposed to start the server but it does not do anything at all. It just return to a prompt again.
#app.py
import web
urls = (
'/', 'index'
)
app = web.application(urls, globals())
class index:
def GET(self):
greeting = "Hello world"
return greeting
if __name__ == "__main__":
app.run
I installed lpthw using pip first.
$pip install lpthw.web
When I run the file it gave me ImportError: no 'web' exists
So I installed webpy myself using with help of http://webpy.org/install
And now I'm getting no result at all.
Python I am using is Python 2.7.8 : Anaconda 2.1.0 . So there must be no conflict. Any suggestions? Thank you.
So, I fixed it successfully but adding parenthesis to the function app.ru()
#app.py
....
greeting = "Hello world"
return greeting
if __name__ == "__main__":
app.run
Related
Python 2.7
Django 1.10
settings.ini file(located at "/opts/myproject/settings.ini"):
[settings]
DEBUG: True
SECRET_KEY: '5a88V*GuaQgAZa8W2XgvD%dDogQU9Gcc5juq%ax64kyqmzv2rG'
On my django settings file I have:
import os
from ConfigParser import RawConfigParser
config = RawConfigParser()
config.read('/opts/myproject/settings.ini')
SECRET_KEY = config.get('settings', 'SECRET_KEY')
DEBUG = config.get('settings', 'DEBUG')
The setup works fine locally, but when I deploy to my server I get the following error if I try run any django management commands:
ConfigParser.NoSectionError: No section: 'settings'
If I go into Python shell locally I type in the above imports and read the file I get back:
['/opts/myproject/settings.ini']
On server I get back:
[]
I have tried changing "confif.read()" to "config.readfp()" as suggested on here but it didn't work.
Any help or advice is appreciated.
I created several servers, without any issue, with the stack nginx - uwsgi - flask using virtualenv.
with the current one uwsgi is throwing the error cannot import name "appl"
here is the myapp directory structure:
/srv/www/myapp
+ run.py
+ venv/ # virtualenv
+ myapp/
+ init.py
+ other modules/
+ logs/
here is the /etc/uwsgi/apps-avaliable/myapp.ini
[uwsgi]
# Variables
base = /srv/www/myapp
app = run
# Generic Config
# plugins = http, python
# plugins = python
home = %(base)/venv
pythonpath = %(base)
socket = /tmp/%n.sock
module = %(app)
callable = appl
logto = %(base)/logs/uwsgi_%n.log
and this is run.py
#!/usr/bin/env python
from myapp import appl
if __name__ == '__main__':
DEBUG = True if appl.config['DEBUG'] else False
appl.run(debug=DEBUG)
appl is defined in myapp/ _ init _ .py as an instance of Flask()
(underscores spaced just to prevent SO to turn them into bold)
I accurately checked the python code and indeed if I activate manually the virtualenvironment and execute run.py manually everything works like a charm, but uwsgi keeps throwing the import error.
Any suggestion what should I search more ?
fixed it, it was just a read permissions issue. The whole python app was readable by my user but not by the group, therefore uwsgi could not find it.
This was a bit tricky because I deployed successfully many time with the same script and never had permissions issues
I am sorry for the dumb question, but I have been banging my head against a wall for past two hours.
I want to use relative imports and my project structure looks like this:
auto_testing
+ tests
+ __init__.py
+ my_module.py
+ src
+ __init__.py
+ my_module.py
+ __init__.py
The contents of tests/my_module.py are:
import unittest
from src.my_module import MyClass
class TestMyClass(unittest.TestCase):
def setUp(self):
self.inst = MyClass()
def test_division_by_zero(self):
self.assertRaises(ZeroDivisionError, self.inst.divide, 1, 0)
def run_tests():
unittest.main()
if __name__ == '__main__':
run_tests()
Then I run commands in terminal
cd auto_testing
python3 -m tests.my_module
and get Error while finding spec for 'tests.my_module.py' (<class 'AttributeError'>: 'module' object has no attribute '__path__').
When I run
cd auto_testing
python3
import tests.my_module
tests.my_module.run_tests()
I get Ran 0 tests (why no tests were found should be another question, but the point is, that MyClass is imported properly and unittest.main() seems to be called; checked with print).
Should I go sleeping for I missed something very simple?
I should have went sleeping.
The commands I needed to run were:
cd auto_testing
python3 -m unittest tests/my_module.py
This also runs my tests.
I have installed qpydjango on my android phone and the sample project works perfectly. I have been trying start a new project(django website) but have not succeeded.
I've tried running "django-admin.py startproject mysite" but I get this error
syntax error or django is not defined.
Since I haven't gotten any answer from anyone I come up with the following in case anyone has the same question.
#qpy:console
import os
import os.path
projectname=raw_input("Enter project name: ")
projectdir="/mnt/sdcard/com.hipipal.qpyplus/projects/"+projectname
if os.path.exists(projectdir):
print "sorry project '"+projectname+"' already exists"
else:
print "Starting django project '"+projectname+"'"
os.makedirs(projectdir)
os.system("cd /mnt/sdcard/com.hipipal.qpyplus/lib/python2.7/site-packages/django/bin && python django-admin.py startproject "+projectname+" "+projectdir)
appname=raw_input("Enter your first app name: ")
print "Starting the first django app '"+appname+"'"
os.system("cd "+projectdir+" && python manage.py startapp "+appname)
print "You project and first app started successfully"
print "Find the project here '"+projectdir+"'"
print " and edit settings.py as necessory"
print " then run python manage.py runserver to start"
Thank you
download this version of django using pip.
pip install django==1.8.16
It works for me
They run normally when I run the flask app directly but don't compile or replace the address in the template when run under uWSGI.
How can I debug this?
EDIT:
code:
assets = Environment(app)
...
if __name__ == "__main__":
assets.register(YAMLLoader(os.path.join(DIR,"assets.yml")).load_bundles())
if os.environ.get("DEBUG_FLASK"):
app.run()
else:
app.run(debug=True)
assets.yml:
style_css:
filters: less
output: css/style.css
contents:
- css/style.less
Turns out uwsgi does its own thing with app variable to run the webapp and doesn't run the script as __main__ so
assets.register(YAMLLoader(os.path.join(DIR,"assets.yml")).load_bundles())
never got called. I moved it out of the
if __name__ == "__main__":
block. And it worked.