add cwd to sys.path
Problem
I want to run kopf without the need to define cwd/pwd in PYTHONPATH. I'm not sure why it's not there, might be related to venv.
Proposal
just make in automatic.
Code
import sys
import os
cwd = os.getcwd()
if not cwd in sys.path:
sys.path.append(cwd)
### Additional information
_No response_
It is not about Kopf, it is about running a generic Python app and is (or is not) an issue of Python. Afair, this was the default behavor for years, but they reverted this behaviour — presumably, for security reasons. It is not up to a specialised niche framework to override that general approach of Python. The app developers can do this if they want, as they control the whole environment.
Also, if kopf is installed as a package normally, it will run without any path manipulation — because it (i.e. pip/poetry/whatnot) installs a bin script named kopf. I did not get what exactly is not working in your example.
when running kopf with -m some.module (assuming ./some/module dir exists and all paths include __init__.py) the import fails since PWD is not in path.
i didn't know that behavior has changed, and I agree that it shouldn't be overridden in the framework - but maybe we can add that as a cli flag
CLI already allows this, basically in any shell:
PYTHONPATH=. kopf run -m some.module
PYTHONPATH=/home/…/dir1:/opt/myoperator/dir2 kopf run -m some.module
Not specific to Kopf, this is a Python feature:
- https://docs.python.org/3/using/cmdline.html#envvar-PYTHONPATH