Using Flox in a new project
Getting started with Flox is super simple. First, create and/orcd into your project directory.
Select a Python interpreter
To begin, we need a Python interpreter. For this example, we will be using Python 3.11. Search for the version of Python your project requires, omitting the dot between major and minor numbers:flox show command:
flox install without a version specified:
flox upgrade is run in this environment, the version of Python will be upgraded to the latest version available in the catalog matching those major and minor numbers. If you wish to pin your Flox environment to a specific release of Python, you can specify it in the flox install command:
Add Python packages
Python projects often require a collection of packages in addition to an interpreter. Often these are installed usingpip, but they can also be acquired from the Flox Catalog. This will allow them to be locked in the Flox Manifest alongside the other packages in your environment.
To find these Python packages in the Flox Catalog, use the same flox search syntax from before:
pythonXXXPackages. in their package name, with XXX matching the version of Python you have installed into your environment. To install them, use the entire package name including the prefix:
Activate the new environment
Once the packages have been installed, activate the new environment:Using Flox in an existing project
If you are working with an existing project that is already configured for Python - e.g. it has arequirements.txt or pyproject.toml - Flox provides an automated environment setup flow.
For this example we will clone the eralchemy repo, which already contains Python configuration:
Auto-initialize the environment
Once inside theeralchemy project directory, initialize a new Flox environment:
pyproject.toml or requirements.txt file inside a project directory, it automatically runs this wizard.
Here’s what each option does:
- “Yes” builds the environment using the
pyproject.tomlfile; - “No” skips automatic setup. You can use
piporpoetrywithpyproject.tomlto build your environment; - “Show” previews the configuration you’d get by selecting “Yes,” allowing you to vet your environment’s setup.
flox edit. For future projects, you can automate this setup process for Python environments by running:
Add system packages
The only dependency frompyproject.toml that Flox did not install for us is Graphviz, an open source tool for creating and visualizing graphs.
To do this, we could run flox edit and add graphviz to the [install] section of our environment’s software manifest, but it’s just as easy to install it from the command line. So let’s do that instead:
Activate and verify the environment
First let’s activate the environment:eralchemy is available within your environment.
Build with Flox
Not only can you develop your software with Flox, but you can build it as well. See the builds concept page for more details.Manifest builds
Pip
For Python projects usingpip, a build looks like installing the project to the $out directory.
. to indicate that you’re installing the package in the
current directory.
If you’re working in a repository with multiple packages in subdirectories,
you would replace . with the path to the package sources.
Poetry
For Poetry and tools that create a virtual environment for you, a build entails installing the virtual environment to$out.
Poetry in particular does not allow you to choose the location (or name) of the virtual environment directory itself, but it does allow you to configure the parent directory of the virtual environment.
The build command shown below uses environment variables to tell Poetry to use the $out directory as the parent of the virtual environment.
After running poetry install you should have a directory structure that looks like this:
$out/bin directory yourself to ensure that the build output adheres to the Filesystem Hierarchy Standard (FHS).
You also need to install a Python interpreter and add it to runtime-packages so that the virtual environment can reference it.
The full manifest for a build using Poetry is shown below: