Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Adapt. Enjoy. Survive.


devel / comp.lang.python / poetry script fails to find module

SubjectAuthor
* poetry script fails to find moduleLoris Bennett
+* Re: poetry script fails to find moduleLoris Bennett
|`* Re: poetry script fails to find moduleStefan Ram
| `- Re: poetry script fails to find moduleLoris Bennett
`* Re: poetry script fails to find moduleStefan Ram
 `* Re: poetry script fails to find moduleLoris Bennett
  `* Re: poetry script fails to find moduleStefan Ram
   `- Re: poetry script fails to find moduleLoris Bennett

1
poetry script fails to find module

<877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23424&group=comp.lang.python#23424

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: poetry script fails to find module
Date: Thu, 28 Jul 2022 15:14:29 +0200
Organization: Freie Universitaet Berlin
Lines: 96
Message-ID: <877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de I46GpIgULdCvjVdCvIZv4wFT8OO69l3GvfQtHItg7f0Jhz
Cancel-Lock: sha1:i3y8h2qLLFVxv0NFDRarWcU0syk=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Thu, 28 Jul 2022 13:14 UTC

Hi,

The following is a little bit involved, but I hope can make the problem clear.

Using poetry I have written a dummy application which just uses to typer
to illustrate a possible interface design. The directory structure is a
follows:

$ tree -P *.py
.
|-- dist
|-- stoat
| |-- hpc
| | |-- database.py
| | |-- group.py
| | |-- __init__.py
| | |-- main.py
| | |-- owner.py
| | `-- user.py
| |-- __init__.py
| |-- main.py
| `-- storage
| |-- database.py
| |-- group.py
| |-- __init__.py
| |-- main.py
| |-- owner.py
| |-- share.py
| `-- user.py
`-- tests
|-- __init__.py
`-- test_stoat.py

With in the poetry shell I can run the application successfully:

$ python stoat/main.py hpc user --help
Usage: main.py hpc user [OPTIONS] COMMAND [ARGS]...

manage HPC users

Options:
--help Show this message and exit.

Commands:
add add a user
remove remove a user

I then install this in a non-standard path (because the OS Python3 is
3.6.8) and can run the installed version successfully:

$ PYTHONPATH=/trinity/shared/zedat/lib/python3.9/site-packages python /trinity/shared/zedat/lib/python3.9/site-packages/stoat/main.py hpc user --help
Usage: main.py hpc user [OPTIONS] COMMAND [ARGS]...

manage HPC users

Options:
--help Show this message and exit.

Commands:
add add a user
remove remove a user

However, poetry creates a script 'stoat' from the entry

[tool.poetry.scripts]
stoat = "stoat.main:main"

in pyproject.toml, which looks like

#!/trinity/shared/easybuild/software/Python/3.9.6-GCCcore-11.2.0/bin/python3.9
# -*- coding: utf-8 -*-
import re
import sys
from stoat.main import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())

If I run that I get

$ PYTHONPATH=/trinity/shared/zedat/lib/python3.9/site-packages stoat hpc user --help
Traceback (most recent call last):
File "/trinity/shared/zedat/bin/stoat", line 5, in <module>
from stoat.main import main
File "/trinity/shared/zedat/lib/python3.9/site-packages/stoat/main.py", line 3, in <module>
import hpc.main
ModuleNotFoundError: No module named 'hpc'

Why is the module 'hpc' not found by the poetry script?

Cheers,

Loris

--
This signature is currently under construction.

Re: poetry script fails to find module

<87v8rhb9yq.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23425&group=comp.lang.python#23425

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: poetry script fails to find module
Date: Thu, 28 Jul 2022 16:14:37 +0200
Organization: Freie Universitaet Berlin
Lines: 109
Message-ID: <87v8rhb9yq.fsf@hornfels.zedat.fu-berlin.de>
References: <877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de pVErEGHeYP4vzeKL1bkmlgTCzT4YN5KD6Nygw5MlTPklAO
Cancel-Lock: sha1:FlBuHN2CeGrW+KVmBTZupQjacfc=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Thu, 28 Jul 2022 14:14 UTC

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:

> Hi,
>
> The following is a little bit involved, but I hope can make the problem clear.
>
> Using poetry I have written a dummy application which just uses to typer
> to illustrate a possible interface design. The directory structure is a
> follows:
>
> $ tree -P *.py
> .
> |-- dist
> |-- stoat
> | |-- hpc
> | | |-- database.py
> | | |-- group.py
> | | |-- __init__.py
> | | |-- main.py
> | | |-- owner.py
> | | `-- user.py
> | |-- __init__.py
> | |-- main.py
> | `-- storage
> | |-- database.py
> | |-- group.py
> | |-- __init__.py
> | |-- main.py
> | |-- owner.py
> | |-- share.py
> | `-- user.py
> `-- tests
> |-- __init__.py
> `-- test_stoat.py
>
> With in the poetry shell I can run the application successfully:
>
> $ python stoat/main.py hpc user --help
> Usage: main.py hpc user [OPTIONS] COMMAND [ARGS]...
>
> manage HPC users
>
> Options:
> --help Show this message and exit.
>
> Commands:
> add add a user
> remove remove a user
>
> I then install this in a non-standard path (because the OS Python3 is
> 3.6.8) and can run the installed version successfully:
>
> $ PYTHONPATH=/trinity/shared/zedat/lib/python3.9/site-packages python /trinity/shared/zedat/lib/python3.9/site-packages/stoat/main.py hpc user --help
> Usage: main.py hpc user [OPTIONS] COMMAND [ARGS]...
>
> manage HPC users
>
> Options:
> --help Show this message and exit.
>
> Commands:
> add add a user
> remove remove a user
>
> However, poetry creates a script 'stoat' from the entry
>
> [tool.poetry.scripts]
> stoat = "stoat.main:main"
>
> in pyproject.toml, which looks like
>
> #!/trinity/shared/easybuild/software/Python/3.9.6-GCCcore-11.2.0/bin/python3.9
> # -*- coding: utf-8 -*-
> import re
> import sys
> from stoat.main import main
> if __name__ == '__main__':
> sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
> sys.exit(main())
>
> If I run that I get
>
> $ PYTHONPATH=/trinity/shared/zedat/lib/python3.9/site-packages stoat hpc user --help
> Traceback (most recent call last):
> File "/trinity/shared/zedat/bin/stoat", line 5, in <module>
> from stoat.main import main
> File "/trinity/shared/zedat/lib/python3.9/site-packages/stoat/main.py", line 3, in <module>
> import hpc.main
> ModuleNotFoundError: No module named 'hpc'
>
> Why is the module 'hpc' not found by the poetry script?

Never mind, I worked it out. I had to replace

import hpc.main

with

import stoat.hpc.main

However, this raises the question of why it worked in the first place
in the poetry shell.

Cheers,

Loris

--
This signature is currently under construction.

Re: poetry script fails to find module

<path-20220728153223@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23426&group=comp.lang.python#23426

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: poetry script fails to find module
Date: 28 Jul 2022 14:33:09 GMT
Organization: Stefan Ram
Lines: 7
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <path-20220728153223@ram.dialup.fu-berlin.de>
References: <877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de> <87v8rhb9yq.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de aGtP9L1OyEiFcEcf0z/hpw5vnyPlRYVyYo97/d3I4eyTI1
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Thu, 28 Jul 2022 14:33 UTC

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>However, this raises the question of why it worked in the first place
>in the poetry shell.

It might have had a different or extended sys.path.

Re: poetry script fails to find module

<87r1241lre.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23438&group=comp.lang.python#23438

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: poetry script fails to find module
Date: Fri, 29 Jul 2022 08:22:13 +0200
Organization: Freie Universitaet Berlin
Lines: 36
Message-ID: <87r1241lre.fsf@hornfels.zedat.fu-berlin.de>
References: <877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de>
<87v8rhb9yq.fsf@hornfels.zedat.fu-berlin.de>
<path-20220728153223@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de 7A/Sg+BAdzbdGLPviANw9Q3n67Fn03i9Tm2Q1pNyi3UJIE
Cancel-Lock: sha1:hMPdxU+ZvcT3WCS+HaMXfdeGlgQ=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Fri, 29 Jul 2022 06:22 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>>However, this raises the question of why it worked in the first place
>>in the poetry shell.
>
> It might have had a different or extended sys.path.

In the poetry shell sys.path has this additional path

/home/loris/gitlab/stoat

The module not being found was

/home/gitlab/stoat/stoat/hpc/main.py

But if I run

[~/gitlab/stoat] $ python stoat/main.py hpc user --help

wouldn't

import hpc.main

still fail? Or is it because I am calling

stoat/main.py

and so Python looks for 'hpc' relative to the second 'stoat' directory?

Confused,

loris

--
This signature is currently under construction.

Re: poetry script fails to find module

<modules-20220729081757@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23439&group=comp.lang.python#23439

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: poetry script fails to find module
Date: 29 Jul 2022 07:21:18 GMT
Organization: Stefan Ram
Lines: 18
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <modules-20220729081757@ram.dialup.fu-berlin.de>
References: <877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 0tLfzSyqAi+NAwBGDU+Efwjd67zeHIYMDRmy1Q49LaxXex
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 29 Jul 2022 07:21 UTC

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>Why is the module 'hpc' not found by the poetry script?

I have tried to execute the following sequence of shell
commands to understand your problem. Here they all worked
without error messages. Warning: Some of these commands might
alter your directories or data, so only execute them if you
are aware of their meaning/consequences and agree with them!

mkdir stoat
mkdir stoat/hpc
echo import hpc.main >stoat/main.py
echo >stoat/hpc/main.py
python3 stoat/main.py
cd stoat
python3 main.py

Re: poetry script fails to find module

<87edy4qpak.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23441&group=comp.lang.python#23441

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: poetry script fails to find module
Date: Fri, 29 Jul 2022 10:46:43 +0200
Organization: Freie Universitaet Berlin
Lines: 63
Message-ID: <87edy4qpak.fsf@hornfels.zedat.fu-berlin.de>
References: <877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de>
<modules-20220729081757@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de dlem1O2277i2P6C9qePWCw4/1TiWcyOG4dkj/O70oIBqLs
Cancel-Lock: sha1:RzucfXMnP6GF6L6WJu8Xgi63lh0=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Fri, 29 Jul 2022 08:46 UTC

Hi Stefan,

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>>Why is the module 'hpc' not found by the poetry script?
>
> I have tried to execute the following sequence of shell
> commands to understand your problem. Here they all worked
> without error messages. Warning: Some of these commands might
> alter your directories or data, so only execute them if you
> are aware of their meaning/consequences and agree with them!

I do know what the commands do. However, just to be on the safe side,
but mainly in honour of a classic dad-joke, I changed 'stoat' to
'weasel'.

> mkdir stoat
> mkdir stoat/hpc
> echo import hpc.main >stoat/main.py
> echo >stoat/hpc/main.py
> python3 stoat/main.py
> cd stoat
> python3 main.py

I guess with the production version, the package

stoat

is visible in sys.path and thus the subpackages have to be referred to
via the main package, e.g.

import stoat.hpc.main

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? That doesn't seem likely to
me, but I am already confused.

Cheers,

Loris

--
This signature is currently under construction.

Re: poetry script fails to find module

<path-20220729101040@ram.dialup.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23442&group=comp.lang.python#23442

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: poetry script fails to find module
Date: 29 Jul 2022 09:12:18 GMT
Organization: Stefan Ram
Lines: 28
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <path-20220729101040@ram.dialup.fu-berlin.de>
References: <877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de> <modules-20220729081757@ram.dialup.fu-berlin.de> <87edy4qpak.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de RKO0qrTnHhIn+tVcdXKLTwnmkfJqWAhW4SHel8sxSRlj+y
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 29 Jul 2022 09:12 UTC

"Loris Bennett" <loris.bennett@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".)

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).

Re: poetry script fails to find module

<87a68sqn6r.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=23443&group=comp.lang.python#23443

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.bennett@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: poetry script fails to find module
Date: Fri, 29 Jul 2022 11:32:12 +0200
Organization: Freie Universitaet Berlin
Lines: 43
Message-ID: <87a68sqn6r.fsf@hornfels.zedat.fu-berlin.de>
References: <877d3xcrbe.fsf@hornfels.zedat.fu-berlin.de>
<modules-20220729081757@ram.dialup.fu-berlin.de>
<87edy4qpak.fsf@hornfels.zedat.fu-berlin.de>
<path-20220729101040@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de AVShOlOmBECjwj2lY+rGugcaLZj/WbDZWpjyegXo6GzcpA
Cancel-Lock: sha1:Trpo9nVCL0AARdZADhH0m5y+4N4=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Fri, 29 Jul 2022 09:32 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> "Loris Bennett" <loris.bennett@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.


devel / comp.lang.python / poetry script fails to find module

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor