Creating a Python Environment
Imagine you have been assigned a new project which is completely based on Python or you have started your programming career and chose Python as your first programming language, You may have followed some articles and youtube videos. Which suggests you to create a python environment before you start working on the project. Any idea why they said so ? Even if you don’t get it do not worry as you are going to learn something new today!!
Now let’s first understand what does this python environment actually means
Consider an example: You have a python project A which has some library dependencies and over the weekend you saw another project (let’s say B) online and want to give it a try. But here comes the problem, Project A uses some libraries and project B uses some of the same libraries but with different versions. Now you want to work on both the projects but cannot as the requirements are clashing for project A and B.
To avoid facing these kind of scenarios we use environments, we setup env_A for project A and env_B for project B. This way no matter what libraries you need for project A will not affect project B in any way. And you can work on both the projects and finish them ( let’s hope you do finish it and not pick another project before finishing it like me 🙂 )
Requirements
Python – Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). For more information, you can visit https://www.python.org
Editor and Terminal – You are free to choose any editor of your choice and you will use your normal command terminal.
Now we are sorted with the requirements let’s write some code,
- Create a folder for the project and open terminal inside the project folder and type the folllowing.
Code language: Python (python)
drive/project_folder:: python -m venv your_env_name
- The above command will create a python environment for you. You have to activate your environment now, do that with this command from the same terminal
Code language: Python (python)
source your_env_name/bin/activate
- Now take a look at your teminal, you would notice something is different , your terminal now looks something like this
Code language: Python (python)
(your_env_name) G:/project_folder>
- Now install all your requirements inside this environment. To deactivate the environment just type Code language: Python (python)
(your_env_name) G:/home/project_folder> deactivate
This way all the requirements of either of your project will not hamper each other. You would be able to work on both of the projects without any problem. If you are just starting out as a python programmer and have no idea what to learn next. I would suggest you to go through the other articles I have written here