Where is the huey consumer configuration? - python

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

Related

Debug a console_script with pycharm

I have a complex python program I'd like to debug where the setup.py has
entry_points=dict(
console_scripts=[
'myprog = myprog.command:myprog_main',
]
)
where the command.py has the logic to accept command so I can run something like
myprog process --config config.yaml
Placing a breakingpoint in pycharm doesn't cause the program to stop, since doing python command.py process --config config.yaml doesn't do anything
I feel this something basic, but I couldn't find a way to debug this (using pycharm)
Let's take jupyter notebook as an example:
In jupyter, it from jupyter_core.command import main, so what I need to do is placing a breakpoint in jupyter_core.command:main.
And then, I need to add a configuration in Pycharm. Script path should be /path/to/jupyter, Parameters should be notebook.
Next, I need to click Debug.
I've done, I reach the breakpoint in jupyter_core.command:main.
In case this answer doesn't work for you, try this:
add a run configuration in PyCharm
configure it with Module name instead of Script path.
Set the Module name to myprog.command
Add the following section to myprog/command.py:
if __name__ == "__main__":
myprog_main()
Sadly, setting the Module name to myprog.command:myprog_main doesn't work.
Set the Working directory to the root of your repo (i.e. the parent of the module myprog such that the imports work).
Note: I used the exact names from OP's question. Please adjust the names of functions, modules, and packages for your problem accordingly.

Django DDP assistance

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.

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!

Python imports confusion

I've got a strange issue with importing a module. I've got a virtualenv setup and one module is available in {env}/lib/python2.6/site-packages/pkgname. There is a __init__.py file and pkgname.py inside.
Now, if I run {env}/bin/python and execute import pkgname.pkgname, it works just fine. But if I create a script in {env}/bin/pkgname.py with contents:
#!{env}/bin/python
import pkgname.pkgname
if __name__ == "__main__":
pkgname.pkgname.run()
this fails trying to import the same file again (since the package and the file have the same name). How can I disable looking in the same directory? Or how can I force the import to first look at the global packages?
Alternatively, what's the "proper" way of doing this? Just for consistency, I'd rather call my startup script the same as the actual package it's trying to run.
Call it pkgname. Done. OK, then it won't start if you doubleclick in it WIndows, but that's usually not a problem.
You can modify sys.path. It's just a list of paths to search and the current folder should be the first entry. Your file should run if you move the current folder to the end of the list. But I general I would not do something like that without a VERY good reason. Isn't it possible to rename your file name.py, runpkgname.py or something like that?

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