Python Virtual Environments! Be safe! How to setup!
You all know that lately I have been learning a bit of Python and one of the very first things that every single course says are: Create all your projects in a Python Virtual Environment. However, what does Virtual Environment mean?
When you do a default installation of Python, you can pretty much use it, install packages and create all your projects and this will work without any problems. The problem begins when a particular project of yours starts to have dependencies of a certain Python package in a certain level. If you run pip upgrade for a package in your global environment all projects that uses that one will get upgraded as well. The outcome from that are two possible results.
1) Your package is upgraded successfully and all projects that relates to that also work after.
2) Your package is upgraded successfully and some of your projects stops to work because of version compatibility.
Virtual Environments for the sake of all!!
Creating Virtual Environments allows you somehow isolate your projects from each other. It also helps to run multiple Python projects in the same machine and it also allows you to have different versions of the same package in different Virtual Environments. For management and modularization I think it is great. So, how do you create a Virtual Environment in your laptop?
First of all you need Python installed. The installation is really simple and https://www.python.org/ may provide you the right information based in your OS. After that, everything you have to do is create the Virtual Environment for your project by tipping the following command in command line (Yes, I am using Windows).
So, what have I done?
1) I created a global folder where all of my projects will be located.
2) I created the folder for a particular Python project
3) I navigate to the folder where Python has been installed
4) From that location I type the command that you can see in the screen to create the virtual environment.
As you also see it, there is neither successfull nor failure message. So how do you confirm that it is working?
1) In our example, go to the folder C:\PythonProjects\sql-scripts
However, in fact to start using them, what you have to do is.
1) Navigate to the sql-scripts folder
2) Open the scripts folder
3) Execute the activate.bat file
Now, you are under your Virtual Environment’s context and this is the place to install all the packages related to this sql-scripts project.
Let’s install one package? I will install mssql-scripter, which I promise later to talk more about it. Actually to install any package is quite simple, you just have to type the following commands.
pip install package-name
How do I make sure the package is installed in my Virtual Environment?
Inside the scripts folder you will see the components.
If you want to compare, go to the same folder in your global Python installation.
I hope you enjoyed the content of this post and as always feedback is more than welcome!
Posted on December 21, 2017, in Python, VirtualPass and tagged create python virtual environments, creating python virtual environments, mssql-scripter, Python, python mssql-scripter, python virtual environments. Bookmark the permalink. Leave a comment.