Install Virtualenv UbuntuIntroduction to VirtualenvIt is a tool to make separate Python environments. Since the 3.3 version of Python, a subset of virtualenv has been developed into the standard library upon the module, i.e., venv. This module doesn't provide every aspect of this library for naming, only some more prominent ones.
The common problem being described is one of the versions, dependencies, and permissions indirectly. Suppose we have software that requires LibFoo version 1, but another software needs 2 version. How can we use these libraries? If we install everything into our host python (for example, python 3.8), it is easy to end up in a condition in which two packages have clashing requirements. Or what if we wish to install software and leave it be? If the software works, any modification in its library or those library versions can break the software. What if we can also not install packages into the global directory, i.e., site-packages, because of not having permission to modify the host Python environment? In each of these situations, virtualenv can aid us. It makes an environment that includes its installation directories that do not share libraries with any other virtualenv environment (and do not access the installed libraries globally). Virtualenv has a common command: It will establish a Python virtual environment of a similar release as virtualenv, installed into the venv subdirectory. The command line tool has some flags that change the behavior of the tool. For a complete list, make sure to inspect CLI flags. These tools work in two different phases:
Note: In our virtualenv, the python is isolated effectively from the python that was utilized to establish it.Discovery of PythonThe python interpreter is the initial thing that we should have to be able to make a virtual environment. It will tell the tool what kind of virtual environment we would like to establish; think of it as implementation, architecture, or version. If we install virtualenv under the 3.8 release of Python, virtualenv will make virtual environments by default that are also of release 3.8. Usually, established python virtual environments are not self-contained. A full python packaging is composed of several files, so it is not able to install the whole python again into a fresh folder. Rather virtual environments are simple shells that include little in themselves and borrow almost everything from the system python. It does mean that if we upgrade our system to python, our virtual environments may break, so we need to be careful. Referring to the python system, the upside of it is that establishing virtual environments can be quick. Here we will define the built-in mechanism. The CLI flag python or p permits us to define a python specifier for what kind of virtual environment we would like; that type is either:
We have the below limitations:
Given the virtualenv specifier will use the below strategy to find/discover the system executable:
Creators Creators are what configure the virtual environment as a reference as opposed to the system python. At the moment, virtualenv has two different types of virtual environments:
Seeders Seeders will install a few seed packages for us (one or more of wheel, setuptools, pip) that enable us to install extra Python packages inside the created virtual environment. There are two different primary seed mechanisms present:
Wheels For installing a seed package by either app-data or pip mechanism, virtualenv requires to inherit a wheel of the destination package. The wheels may be inherited from more than one location:
The automatic behaviour may be deactivated by the no-periodic-update configuration option/flag. To inherit the release date of any package, virtualenv will implement the below:
Users can describe a group of local paths including extra wheels with the command line flag, i.e., extra-search-dir. When finding a wheel to apply, virtualenv performs a lookup inside the below order:
Bundled wheels are each 3 above together. If the locations don't include the requested wheel release or the download flag is set will apply pip download for loading the latest release available in the index server. Distribution Embed Wheels Sometimes, custom distributions want to use their group of wheel versions for distributing rather than a single virtualenv version on PyPi. The cause for it is attempting to keep the system releases of those packages within sync with what virtualenv applies. In such situations, they should patch the virtualenv.seed.wheels.embed module, ensuring to offer the get_embed_wheel function. The MAX, BUNDLE_SUPPORT, and BUNDLE_FOLDER variables are required if they wish to use the test suite of virtualenv to validate. Activators Activation scripts are available, and these will Mangal with the settings of our shell to make sure that commands from inside the Python virtual environment take preference over our system paths. For instance, if invoking pip through our shell given the pip of the system python before activation, when we do the activation, it should refer to the pip virtual environment. Programmatic API Virtualenv provides a CLI level interface only at the moment. If we wish to encounter python environment invocation from inside python, we should be utilized the method, i.e., virtualenv.cli_run; it takes an argument, i.e., args, where we can pass the options the similar way you would from the command line. There are some arguments of the command line interface to establish a virtual environment: Parameters
Returns Creation session object Establish a virtualenv session (similar to cli_run, but it doesn't implement the creation). Use it if we wish to know what the virtual environment will look like but not establish it: Parameters
Returns Creation session object Illustrates the creation session of a virtual environment
CLI interfaceCLI flagsPrimarily, virtualenv is a command-line application. It changes the environment variable inside a shell to establish a separate Python environment, so we will need to have any shell to execute it. We can type inside virtualenv (application's name) pursued by flags that can control its nature. Every option contains sensible defaults, and there is one needed argument, then the path/name of the virtual environment to establish. The command line option's default values can be overridden by the Environment Variables or Configuration file. Environment variables take preference over the values of the configuration file. The options that can be acknowledged to virtualenv, along with a short description and their default values, are mentioned as follows: virtualenv [OPTIONS]
Creator options
Seeder Options
Activators Options
DefaultsEnvironment VariablesAlso, default values may be specified by environment variables. The setting keys are acquired from the command line option, where more than one flag is present first detected wins. Configuration fileVirtualenv finds a standard configuration file, i.e., ini. The same location relies on the operating system we are using as defined by platformdirs definition of the application configuration. The location of the configuration file is mentioned at the completion of the output when the --help option is passed. The setting keys are acquired from the command line option, where more than one flag is present first detected wins. Extend functionalityVirtualenv permits one for extending the builtin functionality by a plugin system. For adding a plugin, we need to:
Installing virtualenv in UbuntuVirtualenv is an open-source and free tool to create separate Python virtual environments. It's widely used by Python developers and programmers to test their distinct application versions with ease. All virtual environments have their Python library (which is the same as the binary version that was used for creating this environment) and can have their independent group of installed Python packages in their site directories. Basic installation tools like pip and setuptools to operate as expected using virtual environments. Step 1: Prerequisites
Step 2: Update Our Server First, we need to update every package in our system by running the sudo apt update command. It will sync our system packages with the currently available versions through the Ubuntu repository. If any system packages are required to be upgraded, we can run the sudo apt upgrade command. Step 3: Installing Python3 We will install the Python3 package with the sudo apt install python3 command in this step. It will download and install this package with each of its dependencies. ![]() Step 4: Installing pip3 We will install the current release of the python package manager with the sudo apt install python-pip3 command because we require to have pip3 to get virtualenv. ![]() Step 5: Installing virtualenv In this step, we will install virtualenv with the pip3 install virtualenv command. If we are using a previous version of pip, we need to run the pip install virtualenv command. The command is shown in the below screenshot: ![]() Step 6: Checking Version We need to run the virtualenv --version command to check the virtualenv installation. ![]() Step 7: Establishing a Virtual Environment We can establish a virtual environment with the virtualenv venv command after the installation process. ![]() Step 8: Activating or Deactivating Environment A virtual environment can be activated or deactivated with a script in its binary directory when it is established. We need to run the source venv/bin/activate command for activating a virtual environment. We can run the deactivate command for deactivating the virtual environment.
Next TopicSafari Ubuntu
|