Reputation: 2006
I have a python file that I want to run using twistd. Inside the file I have:
from parser import Parser
I also have a parser.py
file with a Parser
class inside.
Everything works fine when I invoke the script using:
python myscript.py
But then I call it using:
twistd -y myscript.py
it gives me this error:
from parser import Parser
exceptions.ImportError: cannot import name Parser
What am I missing?
Upvotes: 0
Views: 145
Reputation: 48335
Source files for import as modules need to be discoverable on the Python import path. python
adds the directory containing the script path to the import path. Since you have parser.py
in the same directory as myscript.py
, this makes parser
importable. twistd
(starting with some version in 2016, I think) does not do this (adding the path was considered a security issue similar to the one created by having .
in the PATH
environment variable).
You have a few options.
Best option: Create a setup.py for your project. Create a virtualenv. Install your project into it with pip install -e ...
.
Mediocre option: Add the directory containing your source files to the PYTHONPATH environment variable.
Upvotes: 1