admin管理员组

文章数量:1401122

I went to my project directory and ran

uv init 

thinking that this would create a default virtual environment. It did not. I added in a few dozen dependencies now appearing in myproject.toml But now I want to make this environment available outside uv for example I want to run

 uvicorn myserver.py   

Because I cannot of course run uvicorn uv run myserver.py
So to me the documents are not clear in uv about what the baseline is when doing

 uv venv

I really need to know if it starts from the projects myproject.toml or something else. Will it start from scratch and ignore myproject.toml? overwrite myproject.toml etc? Please clarify. If it does start from scratch and ignore myproject.toml is there anyway to initialise a venv environent with myproject.toml contents?

Update: Can someone clarify if running uv sync will actually create the .venv I only did uv init in the project directory initially. So far its not creating the .venv directory.

I went to my project directory and ran

uv init 

thinking that this would create a default virtual environment. It did not. I added in a few dozen dependencies now appearing in myproject.toml But now I want to make this environment available outside uv for example I want to run

 uvicorn myserver.py   

Because I cannot of course run uvicorn uv run myserver.py
So to me the documents are not clear in uv about what the baseline is when doing

 uv venv

I really need to know if it starts from the projects myproject.toml or something else. Will it start from scratch and ignore myproject.toml? overwrite myproject.toml etc? Please clarify. If it does start from scratch and ignore myproject.toml is there anyway to initialise a venv environent with myproject.toml contents?

Update: Can someone clarify if running uv sync will actually create the .venv I only did uv init in the project directory initially. So far its not creating the .venv directory.

Share Improve this question edited Mar 25 at 13:57 Draco asked Mar 25 at 13:07 DracoDraco 635 bronze badges 7
  • These questions seem to be ones that are better answered by trying things out. Does my answer at python uv module : confusing behaviour have what you need? – InSync Commented Mar 25 at 13:17
  • I dont seem to be able to add comments under the correct answer but still wondering what virtual environment is being used if I only did "uv init" added some packages "uv add fastapi" and then I am able to do uv run myserver.py and it finds the fastapi package? What environment is it finding under if I did not yet do the uv sync? I already checked that no .venv directory exists. – Draco Commented Mar 25 at 13:36
  • In that case, uv will create an environment automatically (at yourproject/.venv by default, though this may change if you configure otherwise). – InSync Commented Mar 25 at 13:38
  • also curious why "uv init" does not create a default virtual env which is kind of what i expected. – Draco Commented Mar 25 at 13:38
  • I am sure I did a "uv init" in existing project directory thinking .venv would be created but this did not happen – Draco Commented Mar 25 at 13:38
 |  Show 2 more comments

1 Answer 1

Reset to default 0

I assume you mean pyproject.toml.

I really need to know if it starts from the projects myproject.toml or something else. Will it start from scratch and ignore myproject.toml?

uv venv creates a blank virtualenv – the only packages it installs are seed packages like pip, when so directed (--seed).

You don't generally need to explicitly do that, since uv works with the convention that a .venv directory in your project directory is the project's virtualenv.

overwrite myproject.toml etc?

No, uv venv doesn't touch your pyproject.toml file.

If it does start from scratch and ignore myproject.toml is there anyway to initialise a venv environent with myproject.toml contents?

uv sync will ensure the project's virtualenv is up to date (creating one if needed). (If you don't have a virtualenv at .venv, and want one to be created and your project's packages installed there, just uv sync.)

Even more helpfully, uv run ... will run a command with the project's virtualenv activated, syncing it beforehand by default.

IOW, uv init (once), uv add (when adding deps), uv run uvicorn myserver.py are all you need in the basic case:

/v/f/n/6/T/tmp.l8JyBCoDzR $ uv init
Initialized project `tmp-l8jybcodzr`
$ uv add uvicorn fastapi[standard]
Using CPython 3.13.2 interpreter at: /opt/homebrew/opt/[email protected]/bin/python3.13
Creating virtual environment at: .venv
Installed 34 packages in 89ms
/v/f/n/6/T/tmp.l8JyBCoDzR (master) $ uv run uvicorn
Usage: uvicorn [OPTIONS] APP
Try 'uvicorn --help' for help.
...

本文标签: