Mailing List Archive

Getting Directory of Command Line Entry Point For Packages
Greetings list,

Let's say i created a package named miaw

miaw also has a cli command called miaw

miaw prints files and folders in the directory it is called in

except that when miaw is used, it prints the files and folders in
site-packages

This is an analogy for a package i have.

Well forgetting about the lines above, how do i get the path from
which miaw the command is called from?

Thanks

Kind Regards,

Abdur-Rahmaan Janhangeer
about <https://compileralchemy.github.io/> | blog
<https://www.pythonkitchen.com>
github <https://github.com/Abdur-RahmaanJ>
Mauritius
--
https://mail.python.org/mailman/listinfo/python-list
Re: Getting Directory of Command Line Entry Point For Packages [ In reply to ]
On 13/11/2021 10.51, Abdur-Rahmaan Janhangeer wrote:
> Greetings list,
>
> Let's say i created a package named miaw
>
> miaw also has a cli command called miaw
>
> miaw prints files and folders in the directory it is called in
>
> except that when miaw is used, it prints the files and folders in
> site-packages
>
> This is an analogy for a package i have.
>
> Well forgetting about the lines above, how do i get the path from
> which miaw the command is called from?

try:

file_path = __file__
print( file_path )

and process file_path as-required.
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: Getting Directory of Command Line Entry Point For Packages [ In reply to ]
On 13/11/2021 10.51, Abdur-Rahmaan Janhangeer wrote:
> Greetings list,
>
> Let's say i created a package named miaw
>
> miaw also has a cli command called miaw
>
> miaw prints files and folders in the directory it is called in
>
> except that when miaw is used, it prints the files and folders in
> site-packages
>
> This is an analogy for a package i have.
>
> Well forgetting about the lines above, how do i get the path from
> which miaw the command is called from?

try:

file_path = __file__
print( file_path )

and process file_path as-required.

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: Getting Directory of Command Line Entry Point For Packages [ In reply to ]
On 13/11/21 10:51 am, Abdur-Rahmaan Janhangeer wrote:
> ow do i get the path from
> which miaw the command is called from?

What exactly do you mean by "called from"?

If you want the user's working directory, os.getcwd() will
give you that.

If you want something else, you'll have to give us more
details.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Getting Directory of Command Line Entry Point For Packages [ In reply to ]
Greetings,

Well since sometimes i have this: https://github.com/shopyo/shopyo
Old versions worked as we are using it for FlaskCon
<https://github.com/flaskcon/traveller>, even newer versions
until sometimes ago.

shopyo has a copy of the project which is a flask app in site-packages.
upon using shopyo new, it creates a copy of the project in the current
folder
then on running say shopyo run, it just passes commandline args flask run
under the hood

Only thing is that os.getcwd is giving the path of site-packages and not
the directory from
which the command is run from. See the run command's source here:
https://github.com/shopyo/shopyo/blob/19dc2ee03ff0a6a0dca3237f80a11478ee2dbe46/shopyo/api/cmd_helper.py#L313

Same goes for other commands. I wanted to know how to get the directory
from which the
command is called from. Thanks


Else i assume you are the author of this article
<https://python-history.blogspot.com/2009/04/and-snake-attacks.html> as i
was compiling a list of articles
<https://www.pythonkitchen.com/python-engineering-articles/>
and guess that it might be.

Kind Regards,

Abdur-Rahmaan Janhangeer
about <https://compileralchemy.github.io/> | blog
<https://www.pythonkitchen.com>
github <https://github.com/Abdur-RahmaanJ>
Mauritius
--
https://mail.python.org/mailman/listinfo/python-list
Re: Getting Directory of Command Line Entry Point For Packages [ In reply to ]
On 13/11/21 7:23 pm, Abdur-Rahmaan Janhangeer wrote:
> os.getcwd is giving the path of site-packages and not
> the directory from
> which the command is run from.

Something must be changing the working directory before getcwd
is called. Once that happens there's no way I know of to find
out what it was. You'll have to capture it earlier in the process
somehow and pass it to where it's needed.

--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
Re: Getting Directory of Command Line Entry Point For Packages [ In reply to ]
> On 13 Nov 2021, at 06:26, Abdur-Rahmaan Janhangeer <arj.python@gmail.com> wrote:
>
> Only thing is that os.getcwd is giving the path of site-packages and not
> the directory from
> which the command is run from.

In which case the code is doing a os.chdir() before the call to os.getwd().
You need to call getwd() before that happens.

Barry
--
https://mail.python.org/mailman/listinfo/python-list
Re: Getting Directory of Command Line Entry Point For Packages [ In reply to ]
Greetings,

This is what I am trying to do:
How to get the getcwd of the directory of where the command is run and not
that
of the file where the cli entrypoint is found. Having the user enter the
absolute path as a cli
argument does not sound nice.

Kind Regards,

Abdur-Rahmaan Janhangeer
about <https://compileralchemy.github.io/> | blog
<https://www.pythonkitchen.com>
github <https://github.com/Abdur-RahmaanJ>
Mauritius
--
https://mail.python.org/mailman/listinfo/python-list