poetry script fails to find module
Loris Bennett
loris.bennett at fu-berlin.de
Fri Jul 29 05:32:12 EDT 2022
ram at zedat.fu-berlin.de (Stefan Ram) writes:
> "Loris Bennett" <loris.bennett at fu-berlin.de> writes:
>>However, in the development environment, if I run
>> python stoat/main.py hpc user --help
>>then is
>> stoat/hpc/main.py
>>being found via
>> import hpc.main
>>because Python looks in
>> stoat
>>as the parent directory of
>> stoat/main.py
>>rather than the current working directory?
>
> When you run "stoat/main.py", the directory of that file
> "main.py" is automatically added at the front of sys.path.
> (This also would happen after "cd stoat; python main.py".)
OK, that explains it. I initially found that a bit odd, but thinking
about it I see that "the directory containing the file being run" is a
more sensible reference point than the current working directory, which
is totally arbitrary.
> Then, when "import hpc.main" is being executed, the system
> will search for "hpc/main.py" in every entry of sys.path
> and will use the first entry wherein it finds "hpc/main.py".
> So it will use the directory of the file "stoat/main.py",
> i.e., the directory "stoat". It finds "stoat/hpc/main.py".
>
> You can call "python" with "-v", and it will show some lines
> with information about the imports executed, including the
> directories used (albeit hidden in a lot of other lines).
That's useful, although in the production case I would have to tweak the
script generated by poetry.
Cheers,
Loris
--
This signature is currently under construction.
More information about the Python-list
mailing list