Anaconda VS Python Virtual Environments
It’s quite clear that most of the developers who use python in their development cycle prefer using virtual environments to install their libraries and dependencies, But why we should create a virtual environment for the project we will work on?
Let me tell you something you already know, python is one of the easiest languages for development, and it’s pretty obvious that we can develop a lot of libraries using it also for specific use, something like Pandas or Numpy which are widely used in Data Science and machine learning domain are developed in python, and guess what, you can develop your own but that’s another topic we may discuss later.
First of all, when you want to start a project using python, you should determine what kind of the project is and based on the diagrams, UMLs, Flowcharts and product owner opinion, you will start writing your own code, but here is the point.
Installing to much libraries may make a conflict between the dependencies the project may not work properly.
In such cases we should use the virtual environments, but what is virtual environments?
Python and Anaconda provides you a tool that you may install using (pip) which is called virtual environments or (venv), it creates a new and clean environment for you, and you can start installing your own libraries that you will use to avoid any kind of conflict that may happen between the libraries, so right now we have two environments, the first one the base one created with python during installation and the other one is the one we created for our project.
Let’s go into code.
First you will need to install python 3.X (X stands for whatever the version you are going install), and after this you will need to write in the terminal or the command line <pip list>
<pip list> will provide a list of all the libraries you installed on the main environment of python. If you didn't find the venv library in the list, you can simply type this command <pip install virtualenv>
Once you created the directory for your project, you will create a new environment using the (venv) library by writing this commands
<cd <directory you desire>>
<python -m venv <the desired name of your environment>>
then you will go to the directory created which is your virtual environment, go to a folder called Scripts and then type in your command line <activate>
Voila, Here is your virtual environment is ready and up to work, you can do whatever you want, it will be your own sandbox or playground.
Whenever you want to exit from the virtual environment you created, you can type deactivate, and like nothing happened at all.
In Aanaconda
Things in anaconda is a little bit different, after installing anaconda from their official website, you will be able to do whatever you want with the command line tools you will have.
By default, anaconda creates a virtual environment on its own, so once you have anaconda you will find something in the anaconda prompt which is (base)
Lets go straight into the commands you will write for creating a virtual environment using anaconda
the first thing that you need to know is that anaconda gives you a command like <conda>, this command gives you the access to a lot of things you can do with anaconda, our main focus is on creating virtual environment. So let’s start
first, go to your working directory and type <conda create -n test python=3.6>
there is something you need to take care about, the first thing is <conda> which initializes the conda command
create: it’s used mainly for creating virtual environment and for me — and that’s a personal opinion — using anaconda is better than using the normal version of python as it gives you the ability to create environment for multiple purposes and it can be a template whether you are going to create an environment for a django app or a flask app, or even an environment that uses some tools like sikit-learn or Tensorflow
-n: stands for the name you are going to give to your virtual environment
python=3.6: is the version of python you want to use, you can type whatever the version you want to use
and simply, hit Enter
you will go with some procedures, asking you for accepting installing some libraries and dependencies.
After you finish all of that, just type <activate test>
let me give a quick tip:
to list all of the anaconda environments you have you can write <conda env list>
in case you want to deactivate the virtual environment you are using, just type <deactivate> and everything will return to default settings.
The last but not least, you can type <conda env remove -n test>, that’s for removing a virtual environment you no longer need.
I want to give some credits to those people who are always helping me in my career journey (Kareem El safty and Mohamed Rashad), I do owe you a lot