Whether you want to run scripts that others have written or you want to develop a project yourself, doing it in a new python environment is the best way. The environment setup should be well managed, or you risk the dependencies of different projects to conflict with each other. And, suddenly you do not know why your software is falling apart. So, to make this problem go away, here are the dos and don’ts of python environment setup.
What is a Python Environment?
If you are a python developer and do not know about python environments then, continue reading. A python environment is like a virtual environment where you can code, test, and run your project without it having to conflict with other projects. When you install a package using pip, it is installed in a separate python environment with an interpreter and other features like standard library, and pre-installed packages.
So, the reason it is preferred to code in virtual environments is keeping all the projects separate and avoid any unnecessary conflicts. While, global environments are best for server management, for scripting, or running code on server.
Need for virtual python environments?
To avoid dependencies, prevent your software from having issues with permissions, and to maintain versions of the software virtual environments are the best solution. While, all this could also be done in global environments but then you would be risking the software with all aforementioned risks.
Additionally, the downside of developing in a global environment is, project 1.1 and project 1.2, python cannot differentiate between the two projects. This is because they are listed in the same directory with the same name.
On top of that, in global environments there are already hundreds of projects. Organization and management is difficult, thus when you want to test your software with a specific package it becomes a mess.
Managing Python Environments with these Best Practices
Use a Virtual Environment
Except for server management and running small scripts, every other project should use a developed virtual environment. You can choose between different package managers based on the features they have to offer. Then carefully select the best suitable environment for your project.
Python coders, If this is your first time using a python environment then Virtualenv is more suitable than any other. Coming with features like code completion and easy switch between environments are just two among many features it offers.
Using the Requirement.txt File
All python programmers for hire should use requirement.txt files and reduce the amount of work you have to do. With the requirement.txt file you can reproduce the environment settings, simply add it to the root directory of your project and you are done. Here is how you can take full advantage of the Requirement.txt file.
- Use pip freeze to output installed packages suitable for a requirements file:
C:\> py -m pip freeze
- Generate a requirements.txt file and then install it into another environment:
env1\bin\python -m pip freeze > requirements.txt
env2\bin\python -m pip install -r requirements.txt
One Virtual Environment for One Project
The best way is to use just one Virtual Environment for One Project, meaning creating a new virtual environment for each new project. This way the dependencies of each project will remain in that environment and you will not face issues.
For projects that have the same requirements and dependencies you can use the same virtual environment for them, like a shared environment. But there is still risks. If one of the projects demands a requirement that poses a threat of breaking the other project then you are doomed.
The space and convenience you have by creating the same environment for multiple projects is not worth the risk.
Additionally, when you can use requirements.txt files to create the same dependencies and setup for the next environment then why take a risk at all.
Activate Python Environment
Just creating is not enough, you have to activate the environment itself. And, one common mistake many python developers make is activating the wrong environment. The problem with this is, the environment you activate is treated as the default environment until you deactivate it, which can easily be done using a deactivate command.
Do Not Use >= for Package Versioning
When versioning your package in a virtual environment remember not to use “>=”. This is a common mistake when using the requirements.txt file, so list your exact package version. Here is an example:
use mypackage==3.2, not mypackage>=3.2.
Python virtual environment is to boost your productivity and avoid conflicts with other projects, so it should not become a hindrance. Above mentioned are the best practices when creating and using virtual environments. Follow these practices but do not stop there because the journey to becoming a great python coder is a longone. If you are searching for a python programmer for hire then vteams is your go to place. Check them out.