Django DDP assistance - python

I'm sorry for this question I am not yet an expert to both django and meteorjs. I am trying to use this django-ddp technology but I am a little stuck on "Start the Django DDP service" on the Example Usage instruction at https://github.com/commoncode/django-ddp
I created a virtualenv,
I created a project named tutorial,
I followed the example usage instructions from the start until,
Every time I tried to run this command (DJANGO_SETTINGS_MODULE=tutorial.settings dddp) in shell I always get a response of "ImportError: No module named tutorial.settings"
P.S.: I even tried to package the project dir but still no luck.

It would seem the issue is that your project isn't on the PYTHONPATH.
I had this issue when I wanted to set DDDP to be called from a executable python file. So, I created a file called run_dddp.py and added this:
#!/usr/bin/env python
import os
import subprocess
if __name__ == "__main__":
new_env = os.environ
new_env['PYTHONPATH'] = '/path/to/your/project'
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tutorial.settings")
subprocess.call(['dddp'], env=new_env)
That adds the location of your project to the path and passes it to dddp.
I suppose you could also just modify the dddp exectuable and add in sys.path.append(/path/to/your/project) there as well or just add it to the path before calling DDDP every time, too. But the file above was just easier for me.

Related

Is it possible to make a PyQt5 app that calls a python script via a subprocess fully self-contained?

Apologies if I am missing something obvious here. I have a PyQt5 app that I've frozen using the awesome fbs package. Within the app, a Python script is called via a PyQt subprocess, i.e. like this:
command = "python LaunchPPTKwin.py"
self.child = QProcess()
self.child.start("cmd.exe /C python LaunchPPTKwin.py")
self.child.waitForFinished(-1)
This works fine when the app is run on the machine on which the app was built. When I bring it to another machine, however, the app runs but the LaunchPPTKwin.py script is never executed. I assume this is because the other machine does not have python installed and/or does not have the LaunchPPTKwin.py script locally. My goal is to create an app so that this will work without the user needing to separately download python or the script, i.e. to make the app totally self contained. Is this possible using fbs?
P.S. Both machines are using Windows 10.
Either python is not installed or python is not in the search path and you had to locate it.
If python were installed, but not in the search path following code should work.
self.child.start("cmd.exe /C start LaunchPPTKwin.py")
If python is not installed at all you could cheat with more effort.
you had to make sure, that your frozen application can handle a command line parameter that allows it to start another script.
It would take the python file.
add the base directory of that file to sys.path strip of the .py suffix of the basename and import it and call it's main function. THis works however only if the script had following lines at its end
def main():
the_code_you_want_to_start
...
if __name__ == "__main__":
main()
It would further only work if all modules, that this file imports are already used by your frozen app.
So you see it's a little complicated, but I did something similiar with a py2exe application and with a pyinstaller application
Figured it out: I just compiled the Python script being called into an executable using Pyinstaller, and then called that executable. So my subprocess call above turned into:
command = "cmd.exe /C LaunchPPTKwin.exe"
self.child = QProcess()
self.child.start(command)
self.child.waitForFinished(-1)

Python: no module named 'bottle-websocket' when running an executable made with PyInstaller, including Eel module

I was playing around with the eel module for Python - gives an opportunity to run HTML + CSS/JS with the python functionality.
Made a simple program, tried to make an executable out of it via PyInstaller.
No errors whatsoever, but when running this executable it crashes on the first line - import eel, telling - there is no module called 'bottle-websocket'.
I checked pip: eel, bottle-websocket are installed. Can't figure out what's the problem. Attachments:
main.py:
import eel
from os import path
eel.init(path.dirname(__file__) + "/web")
eel.start('main.html', block=False)
while True:
eel.sleep(10)
Error:
Picture with the error while I try to start the exe
EDIT:
PyInstaller Log
I was also having this same issue, but I finally fixed it, it was actually very very easy, first of all make sure you are using auto-py-to-exe to package your app.
After inserting necessary details (script file,, extra files etc), you would see an advanced options tab probably below, click on it to expand it, look for the hidden imports label and insert "bottle-websocket" in the input field representing the hidden imports, that's basically all you need to do
I HOPE THIS HELPS
Took me the whole day figuring out the solution, but finally, here it is:
Copy the plugin.py, server.py files from C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bottle_websocket to C:\Users*YOUR_USERNAME*\AppData\Local\Programs\Python\Python36-32\Lib
Make sure you have this line as follows in your *.spec file generated by PyInstaller (FOR PYTHON 3.6 32bit):
datas=[('C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eel\eel.js', 'eel'), ('PATH_TO_YOUR_WEB_FOLDER', 'YOUR_WEB_FOLDER_NAME')]
3)Run this command in cmd: python C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python36-32\lib\site-packages\eel\main.py HERE_SHOULD_BE_THE_PATH_TO_YOUR_WEB_FOLDER
this command will get the 'bottle-websocket' work and will make sure that it complies with the web folder and files.

Python 2.7 to 3.4 Subprocess Issue

I'm calling another program with a piece of code that looks like this:
import subprocess
lc="/package/bin/program --do stuff"
command_list = lc.split()
ljs=subprocess.Popen(command_list,stdout=subprocess.PIPE)
ljs.communicate()[0]
The string works fine at the UNIX command line and the code works in Python 2.7. But, in Python 3.4, I get an error like this:
File "/package/bin/program", line 2, in <module>
from package import module
ImportError: No module named package
"/package/bin/program" is calling a dependency from another file in the package here, which I think is the core issue. I have calls to other programs that are working fine in 3.4.
What's changed in 3.4 that might be causing this?
(Sorry in advance for the cryptic code - I'm calling company internal tools that I can't expose here).
The problem is that the working directory of the subproccess instance is default the directory of bash shell. To set a new working directory, set the cwd argument in your Popen to your working directory.
Here's an example:
subprocess.Popen(['random' '--command'], stdout = subprocess.PIPE, cwd='C:/Path/To/Working/Directory/')
Comments above have been helpful in exploring the issue, but at the end of the day this seems to be some permissions conflict - adding a sudo -u <user> before the command fixes the issue. Still not clear why Py3 requires this and Py2 doesn't, but perhaps I need to explore the issue more closely with other internal users.
Thanks!

Where is the huey consumer configuration?

I'm trying to get running the Huey task queue for python (it's a Celery alternative) and I'm stuck with starting the consumer with main.Configuration (as it's written in the tutorial). I know the huey_consumer is looking for the configfile somewhere in the python, but I cannot get it working according to the tutorial and I don't know why I should write a config file and load it as a module (instead of a file).
When I run huey_consumer.py main.Configuration it returns Unable to import "main".
Huey have this issue written in their common pitalls too, but it's not really helping either.
If there is somebody using Huey, please help me.
I had the same problem and solved it by adding the directory containing main.py to the PYTHONPATH. There was no need to copy huey_consumer.py
So the way this works is it tries to import the module "main". It would be equivalent to opening up a python shell and running:
>>> from main import Configuration
Perhaps you can reply with more information about your environment, the layout of your files, what you're running, etc.
I think you need to add an __init__.py file to the folder that contains main.py, ... else you cannot import anything as python will not consider this folder as a module.
What's the difference between a Python module and a Python package?
See comment by giulio-piancastelli

import problem with twisted.web server

I'm just getting started with twisted.web, and I'm having trouble importing a Python module into a .rpy script.
in C:\py\twisted\mysite.py, I have this:
from twisted.web.resource import Resource
from twisted.web import server
class MySite(Resource):
def render_GET(self, request):
request.write("<!DOCTYPE html>")
request.write("<html><head>")
request.write("<title>Twisted Driven Site</title>")
request.write("</head><body>")
request.write("<h1>Twisted Driven Website</h1>")
request.write("<p>Prepath: <pre>{0}</pre></p>".format(request.prepath))
request.write("</body></html>")
request.finish()
return server.NOT_DONE_YET
and in C:\py\twisted\index.rpy, I have this:
import mysite
reload(mysite)
resource = mysite.MySite()
I ran twistd -n web --port 8888 --path C:\py\twisted in command prompt and the server started successfully. But when I requested localhost:8888 I got a (huge) stack trace originating from an ImportError:
<type 'exceptions.ImportError'>: No module named mysite
I can import the module from the interpreter, and if i just execute index.rpy as a python script, I don't get the import error. The documentation on this subject is a bit vague, it just says "However, it is often a better idea to define Resource subclasses in Python modules. In order for changes in modules to be visible, you must either restart the Python process, or reload the module:" (from here).
Does anyone know the proper way to do this?
Short answer: you need to set PYTHONPATH to include C:\py\twisted.
Long answer...
An rpy script is basically just some Python code, like any other Python code. So an import in a rpy script works just like an import in any other Python code. For the most common case, this means that the directories in sys.path are visited one by one, in order, and if a .py file matching the imported name is found, that file is used to define the module.
sys.path is mostly populated from a static definition including things like C:\Python26\Lib\ and from the PYTHONPATH environment variable. However, there's one extra thing worth knowing about. When you run "python", the current working directory is added to the front of sys.path. When you run "python C:\foo\bar\baz.py", C:\foo\bar\' is added to the front ofsys.path. But when you run "twistd ...", nothing useful is added tosys.path`.
This last behavior probably explains why your tests work if you run the rpy script directly, or if you run python and try to import the module interactively, but fail when you use twistd. Adding C:\py\twisted to the PYTHONPATH environment variable should make the module importable when the rpy script is run from the server you start with twistd.

Categories

Resources