#Python – How to create a Virtual Environment in #Windows10 (Easy one, and brain backup!)

Hi!

Quick post today, and mostly a reminder on how to create a Virtual Environment in Python in Windows 10. I’ve been doing this mostly in my Mac and my Raspberry Pi, and I always forget how to do this on Windows, so … I’ll write this post to have this

Install Python3.

Download the installers from the official Python source (see references). I usually install it on the root of my C: hard drive and name it with the version. In example: c:\Python37_64 folder.

Remember to also add this folder and the Scripts folder to the Environment Variables.

Note: Once you start to install tools which uses Python, your OS becomes a nightmare. You will have your own installed python, the version installed with Visual Studio, the one with Visual Studio Code, another one for Anaconda.

I’m not sure if this is a best practice or not, but I usually remove all the other versions and keep mines in the root of the C: drive.

Ok, let’s go on.

For a new virtual environment named “testEnv”, open a command prompt and navigate to the python folder. Then run the command

python -m virtualenv testEnv

After a couple of seconds, the virtual Environment will be installed, and you can use it by run the [activate.bat] script. In this example

  • The virtual environment will be created at [c:\Python37_64\testEnv]
  • The virtual environment activate script will be at [c:\Python37_64\testEnv\Scripts\activate.bat]

So the full command sequence is similar to this one

Now you have your virtual environment up and running and you may want to start to add your own packages or tools. Like in example: Numpy or to list the installed packages

Finally, you may leave the virtual environment with the command

deactivate

Happy Coding!

Greetings @ Toronto

El Bruno

Resources

Advertisements

#VS2019 – Working with #Python Environments now is so cool !

Hi !

Let me start with IANAPU [I am not a Python user], and that’s maybe why, when I need to work and understand what is in my current environment it took me a lot of time to get and deploy the correct tools and the right packages to work with. I’m not a fan of console environments, and if we add this to the 90% of the Python work, that maybe the main problem.

Visual Studio Code is amazing environment to work with Python. So far, is good enough for me and my machine learning devs. I’ll became a brand-new Mac user in the next couple of days, so I hope that everything I’ve learned is the same on Mac.

I was trying some of the new features in Visual Studio 2019 Preview 2, and the options to manage Python environments blown my mind.

Let’s start with a single Python Application in the solution explorer. With 2 clicks I know what I have in my environment: current python version, packages and more.

01 python environment in vs2019 preview 2 solution explorer

With 2 clicks I can add a new environment, where I can choose the Python version, a base Anaconda environment, and tons of options more.

02 new python environment

If we decided to create an Anaconda working environment, we can load our working packages from a file, or from a cool drop-down list.

03 new python environment based on conda

Adding and upgrading packages is done with the usual Visual Studio integrated experience

04 install new packages05 upgrade packages

Of course, we have the option to review the Console logs, to understand the background processes

Virtual environment is being created at '...\Python\PythonApplication1\envBlog01'
Virtual environment was successfully created at '...\Python\PythonApplication1\envBlog01'
----- Creating 'envBlogConda01' -----
Solving environment: ...working... done
## Package Plan ##
  environment location: d:\ProgramData\Anaconda3\envs\envBlogConda01
==> WARNING: A newer version of conda exists. <==
Preparing transaction: ...working... done
  current version: 4.5.4
  latest version: 4.6.1
Please update conda by running
    $ conda update -n base conda
Verifying transaction: ...working... done
Executing transaction: ...working... done
#
# To activate this environment, use:
# > activate envBlogConda01
#
# To deactivate an active environment, use:
# > deactivate
#
# * for power-users using bash, you must source
#
----- Successfully created 'envBlogConda01' -----


---- Installing 'numpy' -----
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/31/7e/8905636f7e4f9b9d7078aa0e701500634f832f145855a11beb098d3b0fb1/numpy-1.16.0-cp36-cp36m-win_amd64.whl (11.9MB)
Installing collected packages: numpy
Successfully installed numpy-1.16.0
You are using pip version 10.0.1, however version 19.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
----- Successfully installed 'numpy' -----


---- Installing 'pip' -----
Collecting pip==19.0.1
  Downloading https://files.pythonhosted.org/packages/46/dc/7fd5df840efb3e56c8b4f768793a237ec4ee59891959d6a215d63f727023/pip-19.0.1-py2.py3-none-any.whl (1.4MB)
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-19.0.1
----- Successfully installed 'pip' -----

Happy Coding!

Greetings @ Toronto

El Bruno

References