Difference between revisions of "Virtual Environments in Python"

From Sustainability Methods
Line 1: Line 1:
 
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.
 
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.
  
==Why use virtual environments?==
+
=Why use virtual environments?=
  
 
In data analysis and science, projects often rely on different libraries and package versions. Virtual environments help you:
 
In data analysis and science, projects often rely on different libraries and package versions. Virtual environments help you:
Line 9: Line 9:
 
• Reproducibility: Share your environment setup with others for consistent results.
 
• Reproducibility: Share your environment setup with others for consistent results.
  
==Creating a virtual environment step by step==
+
=Creating a virtual environment step by step=
  
===Using Anaconda graphic user interface===
+
==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 17: 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.
  
==== Create an environment ====
+
===Create 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).  
  
==== Activate the environment ====
+
===Activate 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.  
 
==== Installing, updating and removing packages ====
 
 
By default, the right table displays all packages that are installed in the active environment. You can use the filter on top of the list to view not installed, updatable, selecte, or all packages instead. Note that you can use the '''Channels''' button to adjust the locations where Navigator searches for packages. See [https://docs.anaconda.com/navigator/tutorials/manage-channels/ Managing channels] for further information on channels. To install a new package, click the '''Apply''' button for an uninstalled package. To update a package, remove a package or install a different package version, click on the checkbox infront of the package name, select the respective action, and click '''Apply'''. 
 
  
 
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)==
 
1. Open your terminal or command prompt.
 
1. Open your terminal or command prompt.
  
Line 41: Line 37:
 
   </syntaxhighlight>
 
   </syntaxhighlight>
  
Replace `myvirtualenviroment` with your desired environment name and `3.9` with the Python version you need.
+
Replace `myvirtualenviroment` with your desired environment name, for example, you can put "november_rain" as the name of your environment, and then, instead of `3.9`, the Python version you need.

Revision as of 21:05, 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.

Why use virtual environments?

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.

Creating a virtual environment step by step

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.

Create 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).

Activate 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)

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.


conda create --name myvirtualenvironment python=3.9

Replace `myvirtualenviroment` with your desired environment name, for example, you can put "november_rain" as the name of your environment, and then, instead of `3.9`, the Python version you need.