Creating Your Own Python Sandbox
How to create virtual environment in command line
Table of contents
No headings in the article.
In the dynamic world of software development, maintaining a clean and organized codebase is paramount to project success. As projects evolve and dependencies accumulate, the risk of conflicts and compatibility issues increases. This is where virtual environments (venvs) step in as saviors, providing isolated sandboxes for each project and ensuring harmony among your project's dependencies.
Virtual environments, as the name suggests, create self-contained Python environments within a project directory. They encapsulate all the project's dependencies, preventing them from interfering with other projects or the global Python installation. This isolation is crucial for maintaining stability and reproducibility, especially when dealing with multiple projects simultaneously
To create a virtual environment in a project folder in Windows:
Open a command-prompt terminal
Navigate to the folder where you want to create your project and virtual environment. example: cd my_project
Create the virtual environment using the venv module. You can name the virtual environment anything you want, but a common name is env. For example:
py -m venv env
This will create a folder called env inside the project folder, which will contain the Python interpreter and the site-packages directory where you can install your dependencies.
Activate the virtual environment by running the activate script inside the env/Scripts folder. For example: env\Scripts\activate
You should see the name of the virtual environment in parentheses before the prompt, indicating that the virtual environment is active. For example: (env) C:\Users\Paul\ninja_project>
You can now install any packages you need for your project using pip. For example, if you want to install Django, you can use the following command: pip install Django
To deactivate the virtual environment, you can use the deactivate command. For example: deactivate
The prompt will return to normal, indicating that the virtual environment is no longer active.