powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"Set up uv in Python
uv
An alternative to approach to using venv to create and manage Python virtual environments is to use the uv Python package manager by Astral. In my experience, the main advantage of using uv is that it is more user friendly. The setting up procedure is very easy. When working in an environment, uv automatically activates it for when you need to add or update a module. uv also maintains a .lock file for keeping track of all modules and their versions. Lastly, uv can also manage different versions of Python. Due to all these user friendly feature, I’ve been noticing I tend to gravitate to uv over venv when setting up environments.
Install uv (windows powershell)
In Windows, uv needs to be installed first. The following code chunk was taken directly from Astral’s docs page.
Restart Positron and verify installation in powershell
Once uv is installed, Positron will need to be restarted (assuming that a Positron Powershell terminal was used to perform the install). If the install was done in a standalone Powershell terminal, then restart Powershell. Then check the installation with the following code.
uv --versionInitialize the root directory with a Python version
Next, navigate to the directory to where the environment will be placed in Powershell and submit the following command. The brackets in 3.12 are to indicate that any other Python version can be used. This procedure will create a new .venv directory, uv.lock, and pyproject.toml files.
uv init --python [3.12]Install modules
Once the environment is initialized then Python modules can be installed with the “add” function in a Powershell window.
uv add requests pandas np jupyter [any other packages]Install modules from GitHub
To install a Python module hosted on GitHub, the following command will do the trick.
# uv add git+https://github.com/owner/repo
uv add git+https://github.com/[carlosivanr]/[dfm_tb]Updating modules from GitHub
To update a Python module hosted on GitHub, enter the following command. This will fetch the latest commit from the specified repository, updates the lockfile, then installs it. Otherwise, the uv add route will not work as it won’t change the package contents.
uv lock --upgrade-package [dfm-tb]
uv syncAnother option is to always track the latest commit. In the pyproject.toml file in the root directory of the virtual environment, add the following two lines. This will map the package name to the user’s GitHub repository.
[tool.uv.sources]
dfm-tb = { git = "https://github.com/carlosivanr/dfm_tb", branch = "main" }After the pyproject.toml file is configured, then the following code will update a package. This is how I current have my workflow setup as it’s convenient for updating the modules that I work on.
uv lock --upgrade-package [dfm-tb]