Difference between revisions of "Virtual Environments in Python"
(2 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
• Reproducibility: Share your environment setup with others for consistent results. | • Reproducibility: Share your environment setup with others for consistent results. | ||
− | ===Using Anaconda graphic user interface=== | + | In this wiki, at the moment, we will use conda as the main package and environment manager. In future entries, other options will be presented. |
− | + | ||
+ | ===Using Anaconda graphic user interface=== | ||
[[File:Anaconda Navigation.png|thumb|right|Source: https://docs.anaconda.com/navigator/overview/]] | [[File:Anaconda Navigation.png|thumb|right|Source: https://docs.anaconda.com/navigator/overview/]] | ||
Line 16: | Line 17: | ||
The Environments page, like Conda, allows you to create and activate environments as well as install, update, and remove packages. | The Environments page, like Conda, allows you to create and activate environments as well as install, update, and remove packages. | ||
− | === | + | ====Creating an environment==== |
Click on the '''Create''' button at the bottom of the Environments list. A dialog box appears where you can name your environment and select the versions of Python and/or R you want to work with. You can also choose to import an environment from your local drive or Anaconda Nucleus ('''Import''' button) or clone an existing environment ('''Clone''' button). | Click on the '''Create''' button at the bottom of the Environments list. A dialog box appears where you can name your environment and select the versions of Python and/or R you want to work with. You can also choose to import an environment from your local drive or Anaconda Nucleus ('''Import''' button) or clone an existing environment ('''Clone''' button). | ||
− | === | + | ====Activating the environment==== |
In the environments list in the left column, click on the arrow button next to the name to activate the environment you want to use. | In the environments list in the left column, click on the arrow button next to the name to activate the environment you want to use. | ||
Line 26: | Line 27: | ||
If you run into problems or want to explore the functions of Anaconda Navigator further, you can access the [https://docs.anaconda.com/navigator/ documentation] via the '''Help''' button in the top-left corner of the Navigator. | If you run into problems or want to explore the functions of Anaconda Navigator further, you can access the [https://docs.anaconda.com/navigator/ documentation] via the '''Help''' button in the top-left corner of the Navigator. | ||
− | ==Using conda and the terminal (recommended)== | + | ===Using conda and the terminal (recommended)=== |
+ | ====Creating the environment==== | ||
1. Open your terminal or command prompt. | 1. Open your terminal or command prompt. | ||
− | 2. Create the environment by specifying a name and the Python version. Remember that is important to consider the Python versions according to your projects and respective libraries you are going to use. | + | 2. Create the environment by specifying a name and the Python version. Remember that is important to consider the Python versions according to your projects and respective libraries you are going to use. You can use any name you want for your environment, however, it is recommende to use one that is related to the projects you will work on. For this example, we will use "sustwiki" as the environment name. |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | conda create --name | + | conda create --name sustwiki python=3.9 |
</syntaxhighlight> | </syntaxhighlight> | ||
− | Replace | + | Replace the python version with the version you need. |
The virtual environments are going to be stored in a folder inside your miniconda or miniforge main directory. However, if you want to store you virtual environment in the specific project folder you are working on, you can use the -p or --prefix option. | The virtual environments are going to be stored in a folder inside your miniconda or miniforge main directory. However, if you want to store you virtual environment in the specific project folder you are working on, you can use the -p or --prefix option. | ||
− | First create your project folder, if don't have any yet | + | First create your project folder, if don't have any yet. For example, we can create one folder named "methodswiki" |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | mkdir | + | mkdir methodswiki |
</syntaxhighlight> | </syntaxhighlight> | ||
Then, go to the folder: | Then, go to the folder: | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | cd | + | cd methodswiki |
</syntaxhighlight> | </syntaxhighlight> | ||
Line 54: | Line 56: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | In this alternative, you don't have to specify the name of your environment as it will take the whole folder path as the name. | + | In this alternative, you don't have to specify the name of your environment as it will take the whole folder path as the name. With our example, it would something like this: "/User/methodswiki/env". |
+ | |||
+ | ====Activating the Environment==== | ||
+ | Before you can use the packages installed in your environment, you need to activate it: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | conda activate sustwiki | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Or, alternatively, if you are using the prefix option: | ||
+ | |||
+ | <syntaxhighlight lang="bash"> | ||
+ | conda activate ./env | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | This command works on all platforms (Windows, macOS, and Linux). Once activated, your terminal prompt will change to indicate you’re working inside the environment. | ||
+ | |||
+ | Now with the environment activated, you can start downloading the packages and libraries you need. | ||
+ | |||
+ | ====Removing the environment==== | ||
+ | Removing a virtual environment is very straightforward. Use one of these commands: | ||
+ | |||
+ | Remove an environment by name: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | conda remove –name sustwiki –all | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Remove an environment by path: | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | conda remove -p ./env –all | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | The flag -all means that you are deleting everything in the environment, from libraries, packages to scripts and notebooks. So you have be very careful. Make a copy of your important scripts and notebooks in another directory. | ||
+ | |||
+ | For more commands and detailed explanation of how to use conda for creating and managing virtual environments, check the documentation here: [https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html Managing virtual environments with conda]. | ||
+ | |||
+ | ==References== | ||
+ | To be added soon | ||
+ | |||
+ | The author of this entry is Gustavo Rodriguez. | ||
− | + | [[Category: Python]] [[Category: Python Setup]] |
Latest revision as of 21:32, 14 September 2024
Virtual environments are isolated spaces where you can install specific versions of Python packages without affecting other projects. Using virtual environments ensures that your projects have the dependencies they need without conflicts. A virtual environment is like your specific workshop where you have all the tools and equipments necessary for a spefici project.
Creating a virtual environment step by step
In data analysis and science, projects often rely on different libraries and package versions. Virtual environments help you:
• Manage Dependencies: Keep project-specific dependencies separate. • Avoid Conflicts: Prevent version clashes between packages. • Reproducibility: Share your environment setup with others for consistent results.
In this wiki, at the moment, we will use conda as the main package and environment manager. In future entries, other options will be presented.
Using Anaconda graphic user interface
The Environments page, like Conda, allows you to create and activate environments as well as install, update, and remove packages.
Creating an environment
Click on the Create button at the bottom of the Environments list. A dialog box appears where you can name your environment and select the versions of Python and/or R you want to work with. You can also choose to import an environment from your local drive or Anaconda Nucleus (Import button) or clone an existing environment (Clone button).
Activating the environment
In the environments list in the left column, click on the arrow button next to the name to activate the environment you want to use.
If you run into problems or want to explore the functions of Anaconda Navigator further, you can access the documentation via the Help button in the top-left corner of the Navigator.
Using conda and the terminal (recommended)
Creating the environment
1. Open your terminal or command prompt.
2. Create the environment by specifying a name and the Python version. Remember that is important to consider the Python versions according to your projects and respective libraries you are going to use. You can use any name you want for your environment, however, it is recommende to use one that is related to the projects you will work on. For this example, we will use "sustwiki" as the environment name.
conda create --name sustwiki python=3.9
Replace the python version with the version you need.
The virtual environments are going to be stored in a folder inside your miniconda or miniforge main directory. However, if you want to store you virtual environment in the specific project folder you are working on, you can use the -p or --prefix option.
First create your project folder, if don't have any yet. For example, we can create one folder named "methodswiki"
mkdir methodswiki
Then, go to the folder:
cd methodswiki
Now create the virtual environment:
conda create -p ./env python=3.9
In this alternative, you don't have to specify the name of your environment as it will take the whole folder path as the name. With our example, it would something like this: "/User/methodswiki/env".
Activating the Environment
Before you can use the packages installed in your environment, you need to activate it:
conda activate sustwiki
Or, alternatively, if you are using the prefix option:
conda activate ./env
This command works on all platforms (Windows, macOS, and Linux). Once activated, your terminal prompt will change to indicate you’re working inside the environment.
Now with the environment activated, you can start downloading the packages and libraries you need.
Removing the environment
Removing a virtual environment is very straightforward. Use one of these commands:
Remove an environment by name:
conda remove –name sustwiki –all
Remove an environment by path:
conda remove -p ./env –all
The flag -all means that you are deleting everything in the environment, from libraries, packages to scripts and notebooks. So you have be very careful. Make a copy of your important scripts and notebooks in another directory.
For more commands and detailed explanation of how to use conda for creating and managing virtual environments, check the documentation here: Managing virtual environments with conda.
References
To be added soon
The author of this entry is Gustavo Rodriguez.