creating an executable of a python application using py2exe - python-2.7

I'm stuck with creating an executable of my python program. My setup for creating an executable is as shown below,
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
project = dict(script="smarttester.py",
dest_base="Smart Tester",
uac_info="requireAdministrator")
console = [project]
setup(
version = "1.0.0",
description = "executable with privileges",
console = console,
options = {'py2exe': {'bundle_files': 1}},
zipfile = None
)
I'm receiving an error showing as,
Traceback (most recent call last):
File "C:\mypath\to\createexe.py", line 14, in <module>
options = {'py2exe': {'bundle_files': 1}},
File "C:\Python27\lib\distutils\core.py", line 162, in setup
raise SystemExit, error
SystemExit: error: MSVCP90.dll: No such file or directory
Looking for a solution.Thanks in advance.

Related

sqlalchemy.exc.UnboundExecutionError: MetaData object is not bound to an Engine or Connection

(env) C:\Users\recur\Desktop\project\app>python manage.py
Traceback (most recent call last):
File "C:\Users\recur\Desktop\project\app\manage.py", line 1, in <module>
from main import app
File "C:\Users\recur\Desktop\project\app\main\__init__.py", line 47, in <module>
Base.metadata.create_all(bind=engine)
File "C:\Users\recur\Desktop\project\env\lib\site-packages\sqlalchemy\sql\schema.py", line 4888, in create_all
bind = _bind_or_error(self)
File "C:\Users\recur\Desktop\project\env\lib\site-packages\sqlalchemy\sql\base.py", line 1658, in _bind_or_error
raise exc.UnboundExecutionError(msg)
sqlalchemy.exc.UnboundExecutionError: MetaData object is not bound to an Engine or Connection. Execution can not proceed without a database to execute against.
What am I doing wrong?
init.py
from .config import Config
engine = Config.SQLALCHEMY_DATABASE_URI
Base = declarative_base()
Base.query = session.query_property()
Base.metadata.create_all(bind=engine)
config.py
import os
import json
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY')
SQLALCHEMY_DATABASE_URI=os.environ.get('DATABASE_URI')
manage.py
from main import app
if name == 'main':
app.run(host='127.0.0.1', port='5000')

Make/force Scrapy to use Python 2.7

On my system, I've both python3 and python 2.7, scrapy only suports python2.7 but by debault my libraries are linked to python 3.4.
I am trying to run a basic example that comes with scrapy documentation that is:
#!/usr/bin/python2.7
import scrapy
class StackOverflowSpider(scrapy.Spider):
name = 'stackoverflow'
start_urls = ['http://stackoverflow.com/questions?sort=votes']
def parse(self, response):
for href in response.css('.question-summary h3 a::attr(href)'):
full_url = response.urljoin(href.extract())
yield scrapy.Request(full_url, callback=self.parse_question)
def parse_question(self, response):
yield {
'title': response.css('h1 a::text').extract()[0],
'votes': response.css('.question .vote-count-post::text').extract()[0],
'body': response.css('.question .post-text').extract()[0],
'tags': response.css('.question .post-tag::text').extract(),
'link': response.url,
}
To run this code, is suggested for the command:
scrapy runspider stackoverflow_spider.py -o top-stackoverflow-questions.json
being stackoverflow_spider.py the above snippet.
The problem is that somehow this is calling python3,and since I am not explicitly calling a python version, not sure how to force the command to use python2.7 libs.
below is the error I get:
Traceback (most recent call last):
File "/usr/local/bin/scrapy", line 9, in <module>
load_entry_point('Scrapy==1.0.5', 'console_scripts', 'scrapy')()
File "/usr/local/lib/python3.4/dist-packages/scrapy/cmdline.py", line 122, in execute
cmds = _get_commands_dict(settings, inproject)
File "/usr/local/lib/python3.4/dist-packages/scrapy/cmdline.py", line 46, in _get_commands_dict
cmds = _get_commands_from_module('scrapy.commands', inproject)
File "/usr/local/lib/python3.4/dist-packages/scrapy/cmdline.py", line 29, in _get_commands_from_module
for cmd in _iter_command_classes(module):
File "/usr/local/lib/python3.4/dist-packages/scrapy/cmdline.py", line 21, in _iter_command_classes
for obj in vars(module).itervalues():
AttributeError: 'dict' object has no attribute 'itervalues'
PS: I have installed scrapy under python 2.7

How to use Flask-APScheduler in existed Flask app

I am trying to get familiar with Flask-APScheduler plug-in through the following sample code: https://github.com/viniciuschiele/flask-apscheduler/blob/master/examples/jobs.py#L1
My project has the following structure:
backend
run.py
application
__init__.py
utilities
__init__.py
views
models
where,
backend>run.py is:
from application import app
app.run(debug=True)
from application import scheduler
scheduler.start()
backend>application>__init__.py is:
from flask import Flask
app = Flask(__name__)
from application.utilities.views import Config
from flask_apscheduler import APScheduler
app.config.from_object(Config())
scheduler = APScheduler()
scheduler.init_app(app)
backend>application>utilities>__init__.py is empty
backend>application>utilities>models.py is empty
backend>application>utilities>views.py is:
class Config(object):
JOBS = [
{
'id': 'job1',
'func': 'application:utilities:views:job1',
'args': (1, 2),
'trigger': {
'type': 'cron',
'second': 10
}
}
]
def job1(a, b):
print(str(a) + ' ' + str(b))
However, I get the following error:
(env)$ python run.py local
Traceback (most recent call last):
File "run.py", line 1, in <module>
from application import app
File "HOME/backend/application/__init__.py", line 106, in <module>
scheduler.init_app(app)
File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 73, in init_app
self.__load_jobs(app)
File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 136, in __load_jobs
self.__load_job(job, app)
File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/flask_apscheduler/scheduler.py", line 159, in __load_job
func = ref_to_obj(func)
File "/home/xxxxxx/.anaconda/envs/env/lib/python2.7/site-packages/apscheduler/util.py", line 264, in ref_to_obj
raise LookupError('Error resolving reference %s: error looking up object' % ref)
LookupError: Error resolving reference application:utilities:views:job1: error looking up object
Does my structure look like ok? Have a placed the right code in the right place? What should I change to make it work?
Your reference should only have one colon (":"). The colon separates the required import from the variable that has to be looked up. So:
'func': 'application.utilities.views:job1'

Scrapy-Scraper Does Not Run

I can run python using Beautiful Soup and Mechanized, but for some reason when I try to use Spray-Scraper it just doesn't work. Here's an example of what happens when I attempt to test the scraper with a tutorial:
Project name & BOT name = "tutorial"
The following scripts are the items.py and settings.py that I used.
items.py
import scrapy
class DmozSpider(scrapy.Spider):
name = "dmoz"
allowed_domains = ["dmoz.org"]
start_urls = [
"http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
"http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
]
def parse(self, response):
filename = response.url.split("/")[-2]
with open(filename, 'wb') as f:
f.write(response.body)
settings.py
BOT_NAME = 'tutorial'
SPIDER_MODULES = ['tutorial.spiders']
NEWSPIDER_MODULE = 'tutorial.spiders'
CMD
C:\Users\Turbo>scrapy startproject tutorial
New Scrapy project 'tutorial' created in:
C:\Users\Turbo\tutorial
You can start your first spider with:
cd tutorial
scrapy genspider example example.com
C:\Users\Turbo>cd tutorial
C:\Users\Turbo\tutorial>scrapy crawl dmoz
Traceback (most recent call last):
File "C:\Python27\Scripts\scrapy-script.py", line 9, in <module>
load_entry_point('scrapy==0.24.4', 'console_scripts', 'scrapy')()
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\cmdline.py"
, line 143, in execute
_run_print_help(parser, _run_command, cmd, args, opts)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\cmdline.py"
, line 89, in _run_print_help
func(*a, **kw)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\cmdline.py"
, line 150, in _run_command
cmd.run(args, opts)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\commands\cr
awl.py", line 58, in run
spider = crawler.spiders.create(spname, **opts.spargs)
File "C:\Python27\lib\site-packages\scrapy-0.24.4-py2.7.egg\scrapy\spidermanag
er.py", line 44, in create
raise KeyError("Spider not found: %s" % spider_name)
KeyError: 'Spider not found: dmoz'
The problem is that you are putting your spider into the items.py.
Instead, create a package spiders, inside it create a dmoz.py and put your spider into it.
See more at Our first Spider paragraph of the tutorial.

django on cherrypy: running server.py within eclipse is OK, but not in a terminal

I followed the tutorial Tango with Django to build up my Django project. And everything runs OK.
The file structure:
tango/
rango/
tango/
wsgi.py
settings.py
manage.py
Now I am trying to deploy the project on the CherryPy server, following this tutorial. The default content of wsgi.py is as follows:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tango.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Now in the same folder as wsgi.py, I create the server.py:
from wsgi import application
import cherrypy
if __name__ == '__main__':
# Mount the application
cherrypy.tree.graft(application, "/")
# Unsubscribe the default server
cherrypy.server.unsubscribe()
# Instantiate a new server object
serve = cherrypy._cpserver.Server()
# Configure the server object
server.socket_host = "0.0.0.0"
server.socket_port = 8080
server.thread_pool = 30
# Subscribe this server
server.subscribe()
# Start the server engine (Option 1 *and* 2)
cherrypy.engine.start()
cherrypy.engine.block()
My question:
If I run server.py within Eclipse (Right click server.py --> Run As --> Python Run), everything works just find. However, if I enter the command $ python server.py in a terminal, the following error messages show up:
Traceback (most recent call last):
File "server.py", line 1, in <module>
from wsgi import application
File "<tangoProject>/tango/wsgi.py", line 14, in <module>
application = get_wsgi_application()
File "<virtualenv>/local/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "<virtualenv>/local/lib/python2.7/site-packages/django/__init__.py", line 20, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
self._setup(name)
File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'tango.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named tango.settings
Note, in the above I used <djangoProject> and <virtualenv> to specify the directories of the project and virtualenv, respectively.
It seemed that the server is not able to find tango/settings.py file. How do I fix it?
In your server.py before importing from wsgi add:
import sys
sys.path.append('/the/path/to/your/project')
Then, the line importing from wsgi change it to:
from tango.wsgi import application