"dramatiq test.py" fails with "'test' is not a package"

129 Views Asked by At

need some help Can't start using dramatiq because some errors

My structure is

projectDirectory/apptest/
  __init__.py  (Eempty file. Not really empty, all lines commented with #)
  test.py 

I'm using Conda environment with dramatiq installed via pip

#--- test.py has the following code

import dramatiq

@dramatiq.actor
def lenstring(txt):
  print(len(txt))

if __name__ == "__main__":
  lenstring.send("hello")

Problem is... 1- Loading environment using conda activate 2- Running dramatiq from apptest in order to start worker

$ dramatiq test.py
[2023-07-26 14:29:19,622] [PID 4664] [MainThread] [dramatiq.MainProcess] [INFO] Dramatiq '1.14.2' is booting up.
[2023-07-26 14:29:18,621] [PID 4666] [MainThread] [dramatiq.WorkerProcess(0)] [ERROR] Failed to import module.
Traceback (most recent call last):
  File "/home/yo/anaconda3/envs/BotAI/lib/python3.8/site-packages/dramatiq/cli.py", line 382, in worker_process
    module, broker = import_broker(args.broker)
  File "/home/yo/anaconda3/envs/BotAI/lib/python3.8/site-packages/dramatiq/cli.py", line 123, in import_broker
    module, broker_or_callable = import_object(value)
  File "/home/yo/anaconda3/envs/BotAI/lib/python3.8/site-packages/dramatiq/cli.py", line 112, in import_object
    module = importlib.import_module(modname)
  File "/home/yo/anaconda3/envs/BotAI/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 970, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'test.py'; 'test' is not a package
[2023-07-26 14:29:19,624] [PID 4664] [MainThread] [dramatiq.MainProcess] [CRITICAL] Worker with PID 4666 exited unexpectedly (code 2). Shutting down...
[2023-07-26 14:29:18,618] [PID 4667] [MainThread] [dramatiq.WorkerProcess(1)] [ERROR] Failed to import module.
Traceback (most recent call last):
  File "/home/yo/anaconda3/envs/BotAI/lib/python3.8/site-packages/dramatiq/cli.py", line 382, in worker_process
    module, broker = import_broker(args.broker)
  File "/home/yo/anaconda3/envs/BotAI/lib/python3.8/site-packages/dramatiq/cli.py", line 123, in import_broker
    module, broker_or_callable = import_object(value)
  File "/home/yo/anaconda3/envs/BotAI/lib/python3.8/site-packages/dramatiq/cli.py", line 112, in import_object
    module = importlib.import_module(modname)
  File "/home/yo/anaconda3/envs/BotAI/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 970, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'test.py'; 'test' is not a package
$

I don't understad how to solve the problem, can someone help me?

I try to start a dramatiq worker, the code is just a simplyfied variation of some example I found on the web. I have a much more complex code to run but can't event start with this simple one (The errors on the complex code is basically the same)

1

There are 1 best solutions below

0
TheTacomancer On

When using the dramatiq command, you shouldn't use the ".py" file extension. Type it out like you would if you were importing something.

In your case it would be: dramatiq test