Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uv run with a different root directory #6733

Closed
haarisr opened this issue Aug 28, 2024 · 20 comments
Closed

uv run with a different root directory #6733

haarisr opened this issue Aug 28, 2024 · 20 comments
Assignees
Labels
question Asking for clarification or support

Comments

@haarisr
Copy link
Contributor

haarisr commented Aug 28, 2024

uv init bird
echo "import bird\n print(bird.hello())" > bird/main.py
uv run bird/main.py
Traceback (most recent call last):
  File "/home/dev/bird/main.py", line 3, in <module>
    print(bird.hello())
AttributeError: module 'bird' has no attribute 'hello'

I would like to run bird/main.py from any directory that the user is in.

If the user could specify the package root or uv could start searching for the pyproject.toml from the script path towards the root directory, that would help fix the issue.

uv version: uv 0.3.2

@JamesMTSloan
Copy link

JamesMTSloan commented Aug 28, 2024

Something like uv -C bird/ run would be useful. Missing this was one of the only slight pain-points I had migrating from Poetry. git also provides an equivalent -C option.

Although, maybe uvx --from <path-to-project> <entry-point> comes close.

@charliermarsh
Copy link
Member

uv --directory exists already -- is this what you're looking for?

@charliermarsh charliermarsh added the question Asking for clarification or support label Aug 28, 2024
@bluss
Copy link
Contributor

bluss commented Aug 28, 2024

uv --directory is hidden, I guess because it is not yet ready?

@zanieb
Copy link
Member

zanieb commented Aug 28, 2024

Oh.. I want this too. Why do we hide it? haha

@charliermarsh
Copy link
Member

I think we hide it because we wanted to change the semantics such that relative paths were resolved relative to the CWD rather than --directory.

@charliermarsh
Copy link
Member

See: #5613. We should unhide it though. I think I originally added it to facilitate benchmarking, and there wasn't consensus on the team at the time.

@zanieb
Copy link
Member

zanieb commented Aug 28, 2024

Oh.. hm.. I guess it might make sense to sort that out since it'll be a breaking change.

@haarisr
Copy link
Contributor Author

haarisr commented Aug 28, 2024

--directory works great. Didn't know that existed, thanks!

I think we hide it because we wanted to change the semantics such that relative paths were resolved relative to the CWD rather than --directory.

This makes more sense to me. You can use shell autocomplete to find the file instead of knowing the script name relative to the project directory

@zanieb
Copy link
Member

zanieb commented Aug 28, 2024

I think it makes sense but it seems non-trivial and I can see people getting confused either way.

@bluss
Copy link
Contributor

bluss commented Aug 28, 2024

I would feature request that it should work like this: #5579 (comment)

But it doesn't have to be --directory that does it. It could be some other flag on uv run with those requested semantics? It is not just about convenience, if the user can't control the current working directory (user "can't control it" here because the --directory has to be equal to the project directory with the uv project) then some commands can't be used correctly at all.

But the current rules for --directory have precedent in cargo -C and git -C.

@zanieb
Copy link
Member

zanieb commented Aug 28, 2024

@bluss to clarify, cargo -C and git -C work like our current behavior?

@bluss
Copy link
Contributor

bluss commented Aug 28, 2024

@zanieb Yep, at least for git I'm sure of that, git says "Run as if git was started in <path> instead of the current working directory"

@charliermarsh
Copy link
Member

We now support --project which accepts a path to a directory like this.

@charliermarsh charliermarsh self-assigned this Sep 24, 2024
@bgolinvaux
Copy link

Thank your for this --project flag and, more generally, for uv 😄
I am in love with uv. It's been a while since a two-letter CLI tool gave me so much joy.

@mdengler
Copy link

mdengler commented Feb 13, 2025

We now support --project which accepts a path to a directory like this.

Can I help to point to some documentation on how to use this? As a poetry user, it's unclear how --project (or --directory) is helping here:

$ uv --version
uv 0.5.25

$ mkdir bird-uv-6733
$ cd bird-uv-6733/
$ mkdir bird
$ echo -e 'def hello():\n  print("hello bird")' > bird/__init__.py
$ mkdir examples
$ echo -e "import bird\nprint(bird.hello())" > examples/example1.py
$ tree .
.
├── bird
│   └── __init__.py
└── examples
    └── example1.py

3 directories, 2 files

$ uv run examples/example1.py 
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733/examples/example1.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'


$ uv --project=$PWD run examples/example1.py
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733/examples/example1.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'

$ uv --project=. run examples/example1.py
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733/examples/example1.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'

$ uv --directory=$PWD run examples/example1.py
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733/examples/example1.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'

$ uv --directory=. run examples/example1.py
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733/examples/example1.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'

$ uv --directory=examples run examples/example1.py
error: Failed to spawn: `examples/example1.py`
  Caused by: No such file or directory (os error 2)

  $ uv --directory=examples run example1.py
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733/examples/example1.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'

I can file a new issue, but it seems something like what I've tried should actually work; that is, changes including:

  1. add $PWD (or --project argument) to PYTHONPATH (this is not done; the script directory gets added to PYTHONPATH, regardless of the value of --project or --directory)
  2. run examples/example1.py

...like this:

$ PYTHONPATH=. uv run examples/example1.py 
hello bird
None

@charliermarsh
Copy link
Member

@mdengler -- please open a new issue articulating your problem rather than commenting on a closed issue.

@mdengler
Copy link

@charliermarsh sure; I opened #11490.

We now support --project which accepts a path to a directory like this.

Can I help to point to some documentation on how to use this?

please open a new issue

I asked for help documenting how the new feature was going to be used, though. Also, the original report does not seem to be resolved with either --project or --directory:

$ mkdir bird-uv-6733-orig
$ cd bird-uv-6733-orig
$ mkdir bird
$ uv init bird
$ echo -e "import bird\nprint(bird.hello())" > bird/main.py
$ uv run bird/main.py
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733-orig/bird/main.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'
$ uv --project=. run bird/main.py
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733-orig/bird/main.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'
$ uv --directory=. run bird/main.py
Traceback (most recent call last):
  File "/home/martin/src/bird-uv-6733-orig/bird/main.py", line 1, in <module>
    import bird
ModuleNotFoundError: No module named 'bird'

Should this issue be re-opened? Or could you point me to where in the documentation it describes how to use --project to address this (#6733) issue?

@mdengler
Copy link

Happy to add to documentation. I was surprised to find no examples of uv run bird/main.py that worked, including with --project=.. I'd just like to help avoid other people getting stuck like I did/am.

@charliermarsh
Copy link
Member

Thanks @mdengler -- I appreciate it. I just find it hard to keep track of outstanding work when there's discussion in closed issues since there's so much activity in the repo. I'm happy to discuss this all in #11490!

@mdengler
Copy link

Understood -- you all are doing tons. Thanks for uv and the great work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for clarification or support
Projects
None yet
Development

No branches or pull requests

7 participants