Actually switch directory with --directory/-C
- [x] I have searched the issues of this repo and believe that this is not a duplicate.
- [x] I have searched the FAQ and general documentation and believe that my question is not already covered.
Feature Request
Previous issues have been raised asking about the behavior of --directory: (https://github.com/python-poetry/poetry/issues/7363, https://github.com/python-poetry/poetry/issues/7507). They have both been closed as "working as expected", so this issue is an official request to change the behavior OR change the flag ~OR update the docs~.
Most GNU tools like make or git will make git -C dir ... equivalent to (cd dir && git ...). Using the same flag implies the same behavior, when it's not currently the case:
this is expected. Poetry just loads the pyproject.toml from the given directory and does not switch to the directory. https://github.com/python-poetry/poetry/issues/7363#issuecomment-1386833452
Ideally, the behavior would be changed to actually change the directory; I don't see why a simple addition of os.chdir in the beginning of the CLI main function would break things.
You don't need to make an "official request" to fix the docs, just submit a merge request - make it happen yourself!
(Reporters in both of the issues that you linked were also invited to fix the docs)
Sure, but ideally the behavior would be changed, so this is a request mostly for that
I think a docs clarification is more likely to succeed - two issues declaring this working-as-designed tells its own story - but: same deal. If you care about this and want it to happen, your best shot is to do it yourself.
I know the issues were closed as "working-as-designed", but I don't actually think that's the case. The original PR (https://github.com/python-poetry/poetry/pull/6810) says it closes https://github.com/python-poetry/poetry/issues/2179, but that issue is asking for the same behavior as this issue, which isn't solved by the current behavior of -C
Imo it should not change the directory, because this would defy the whole option at all. It is especially useful to run a script entrypoint via poetry run your-script from a different folder and still be able to pass files relative to your current directory as arguments that are not relative to the pyproject.toml's directory.
So yes, the feature is working as designed I would assume.
@Makman2 I assume you meant poetry -C ../other/dir/ run entry-point?
I don't think that should be a supported usecase. If you're using another poetry project as a tool for another project, you should add it as a dependency, which will register the entrypoint in the second project so you can just do poetry run entry-point project2-file in the second project
The second project might not be a Python project, and general-purpose tools/helpers not required for building or similar should not be listed in a requirements section normally.
Without this usecase you don't have a chance to sanely invoke a Python entrypoint via poetry and reference any files on command line.
If it's a poetry project, you should certainly add a dev group (or similar) for tools. If it's a non-poetry python project, you should install it to the environment somehow (e.g. requirements-dev.txt or just manually install into the venv venv/bin/pip install ../other/dir)
If it's not a python project, IMO you should install it the same as any other third-party dev tool you use. But as a workaround, you could always do (or write a script to do)
$(poetry -C ../other/dir/ run which entry-point) file1 file2
I also don't understand why is poetry run exception in how it actually handles -C / --directory flag.
When I am outside of MY_PROJECT_DIR and use
poetry --directory=MY_PROJECT_DIR version
it works as expected. But when I run
poetry --directory=MY_PROJECT_DIR run MY_COMMAND
it fails with
Poetry could not find a pyproject.toml file in CURRENT_DIR or its parents
Same error as if I wouldn't use flag -C / --directory at all - it's ignored.
Is there something blocking solution to this issue or is it just too low priority on everyone's rosters?
Still does not work as of version 1.8.3
aa956@desktop02:~/tmp$ ~/projects/toolkit/generate-mask.sh
Poetry could not find a pyproject.toml file in /home/aa956/tmp or its parents
aa956@desktop02:~/tmp$ cat ~/projects/toolkit/generate-mask.sh
#!/bin/bash
PROJECTPATH=~/projects/toolkit
poetry run --directory=$PROJECTPATH python $PROJECTPATH/mask/florence2_sam2_image_mask.py "$@"
aa956@desktop02:~/tmp$ ls ~/projects/toolkit/*.toml
/home/aa956/projects/toolkit/poetry.toml
/home/aa956/projects/toolkit/pyproject.toml
aa956@desktop02:~/tmp$ poetry --version
Poetry (version 1.8.3)
aa956@desktop02:~/tmp$
According to the documentation:
--directory=DIRECTORY (-C): The working directory for the Poetry command (defaults to the current working directory).
Edit: Works with space between --directory and /actual/path and does not work as documented with = between parameter and it's value.
This looks like two bugs then - both that poetry run ignores --directory/-C and that the real usage requires not including = as presented in the docs
And then the overarching issue that --directory/-C is just a sort of --config-file that fetches a pyproject.toml in a different directory, and doesn't actually change the working directory (as documented).
Hey everyone,
because the current behavior seems to confuse several users we are thinking about the following change:
--directory / -Cwill actually change the current working directory. So any relative path will be relative to the given directory.- In addition we provide a
--projectflag, which will work exactly as the--directoryflag now. Meaning thepyproject.tomlfile will be read from the given directory, but any relative path will be interpreted relative to the place where one start the command.
What do you think? Will this meet your needs?
fin swimmer
Thank you @finswimmer!! Love seeing a resolution to this long-lived issue
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.