I will eventually need to install the Jupyter Notebook application, but think I might leave that for awhile. It is not immediately required. Git and such will be installed in the next post.
What I’d like to do now is to create a virtual environment in our “install” folder. Add a Python package or two. Create a “play” project in VS Code based on the “install” folder. Generate a “specification file” for the virtual environment. Export an “environment.yml” file. Kinda doing the samething twice, but in different ways. Create a Github account, set up our “play” project, and finally commit our initial project setup. Should be fun.
Create virtual environment
Open an “Anaconda Prompt (Miniconda)”. Note, use the basic prompt rather than the “Powershell Prompt” because I do not currently know anything about Powershell. I do know a few basic dos commands, which currently gets me by.
Go to your install directory. In my case:
(base) C:\Users\bark>cd /D r:\learn\install
(base) r:\learn\install>
Now to create a virtual environment. For this one I want to use the latest version of Python, 3.8 as of writing, rather than 3.7.x which comes with the conda base virtual environment
.
(base) r:\learn\install>conda create -n base-3.8 python=3.8
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.7.12
latest version: 4.8.2
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: E:\appDev\Miniconda3\envs\base-3.8
added / updated specs:
- python=3.8
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2020.1.1 | 0 125 KB
certifi-2019.11.28 | py38_0 153 KB
openssl-1.1.1d | he774522_4 4.8 MB
pip-20.0.2 | py38_1 1.7 MB
python-3.8.1 |h5fd99cc_8_cpython 16.0 MB
setuptools-45.2.0 | py38_0 531 KB
sqlite-3.31.1 | he774522_0 637 KB
vs2015_runtime-14.16.27012 | hf0eaf9b_1 1.1 MB
wheel-0.34.2 | py38_0 66 KB
wincertstore-0.2 | py38_0 15 KB
------------------------------------------------------------
Total: 25.1 MB
The following NEW packages will be INSTALLED:
ca-certificates pkgs/main/win-64::ca-certificates-2020.1.1-0
certifi pkgs/main/win-64::certifi-2019.11.28-py38_0
openssl pkgs/main/win-64::openssl-1.1.1d-he774522_4
pip pkgs/main/win-64::pip-20.0.2-py38_1
python pkgs/main/win-64::python-3.8.1-h5fd99cc_8_cpython
setuptools pkgs/main/win-64::setuptools-45.2.0-py38_0
sqlite pkgs/main/win-64::sqlite-3.31.1-he774522_0
vc pkgs/main/win-64::vc-14.1-h0510ff6_4
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1
wheel pkgs/main/win-64::wheel-0.34.2-py38_0
wincertstore pkgs/main/win-64::wincertstore-0.2-py38_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
pip-20.0.2 | 1.7 MB | ############################################################################ | 100%
ca-certificates-2020 | 125 KB | ############################################################################ | 100%
vs2015_runtime-14.16 | 1.1 MB | ############################################################################ | 100%
wincertstore-0.2 | 15 KB | ############################################################################ | 100%
setuptools-45.2.0 | 531 KB | ############################################################################ | 100%
sqlite-3.31.1 | 637 KB | ############################################################################ | 100%
certifi-2019.11.28 | 153 KB | ############################################################################ | 100%
wheel-0.34.2 | 66 KB | ############################################################################ | 100%
openssl-1.1.1d | 4.8 MB | ############################################################################ | 100%
python-3.8.1 | 16.0 MB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate base-3.8
#
# To deactivate an active environment, use
#
# $ conda deactivate
(base) r:\learn\install>
I believe the current stable version of Python is 3.8.2. But conda just installed 3.8.1. Not sure why, but I can live with that for now.
Let’s have a look at the currently available conda environments:
(base) r:\learn\install> conda env list
# conda environments:
#
base * E:\appDev\Miniconda3
base-3.8 E:\appDev\Miniconda3\envs\base-3.8
Note the *
on the base environment line. Indicates that this is the currently active environment. Well, almost, apparently the star will also be there if there is no active environment at the moment.
Make sure we’re in our install/play
folder, then activate the newly created environment. And, list the environments once more. Note that the *
has moved and the prompt has changed.
(base) r:\learn\install>conda activate base-3.8
(base-3.8) r:\learn\install> conda env list
# conda environments:
#
base E:\appDev\Miniconda3
base-3.8 * E:\appDev\Miniconda3\envs\base-3.8
(base-3.8) r:\learn\install>
Now let’s install a Python package. I am going to install matplotlib as I expect it will be handy sooner or later. If not I can always uninstall it.
Since we are in an active virtual environment, a straight forward conda install
will add the pacakge to that environment and not any other. If you wish to be certain you can use --name
parameter. And since conda
should install the most recent version compatible with the environment’s version of Python, I won’t bother specifying a particular version of matplotlib.
Note this took a few minutes on my pc and internet connection.
(base-3.8) r:\learn\install>conda install matplotlib
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.7.12
latest version: 4.8.2
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: E:\appDev\Miniconda3\envs\base-3.8
added / updated specs:
- matplotlib
The following packages will be downloaded:
package | build
---------------------------|-----------------
blas-1.0 | mkl 6 KB
cycler-0.10.0 | py38_0 14 KB
freetype-2.9.1 | ha9979f8_1 450 KB
icc_rt-2019.0.0 | h0cc432a_1 6.0 MB
icu-58.2 | ha66f8fd_1 9.4 MB
intel-openmp-2020.0 | 166 1.5 MB
jpeg-9b | hb83a4c4_2 245 KB
kiwisolver-1.0.1 | py38ha925a31_0 51 KB
libpng-1.6.37 | h2a8f88b_0 333 KB
matplotlib-3.1.3 | py38_0 22 KB
matplotlib-base-3.1.3 | py38h64f37c6_0 4.9 MB
mkl-2020.0 | 166 98.9 MB
mkl-service-2.3.0 | py38hb782905_0 50 KB
mkl_fft-1.0.15 | py38h14836fe_0 119 KB
mkl_random-1.1.0 | py38hf9181ef_0 243 KB
numpy-1.18.1 | py38h93ca92e_0 6 KB
numpy-base-1.18.1 | py38hc3f5095_1 3.8 MB
pyparsing-2.4.6 | py_0 64 KB
pyqt-5.9.2 | py38ha925a31_4 3.2 MB
python-dateutil-2.8.1 | py_0 224 KB
qt-5.9.7 | vc14h73c81de_0 72.5 MB
sip-4.19.13 | py38ha925a31_0 262 KB
six-1.14.0 | py38_0 27 KB
tornado-6.0.3 | py38he774522_3 601 KB
zlib-1.2.11 | h62dcd97_3 110 KB
------------------------------------------------------------
Total: 203.0 MB
The following NEW packages will be INSTALLED:
blas pkgs/main/win-64::blas-1.0-mkl
cycler pkgs/main/win-64::cycler-0.10.0-py38_0
freetype pkgs/main/win-64::freetype-2.9.1-ha9979f8_1
icc_rt pkgs/main/win-64::icc_rt-2019.0.0-h0cc432a_1
icu pkgs/main/win-64::icu-58.2-ha66f8fd_1
intel-openmp pkgs/main/win-64::intel-openmp-2020.0-166
jpeg pkgs/main/win-64::jpeg-9b-hb83a4c4_2
kiwisolver pkgs/main/win-64::kiwisolver-1.0.1-py38ha925a31_0
libpng pkgs/main/win-64::libpng-1.6.37-h2a8f88b_0
matplotlib pkgs/main/win-64::matplotlib-3.1.3-py38_0
matplotlib-base pkgs/main/win-64::matplotlib-base-3.1.3-py38h64f37c6_0
mkl pkgs/main/win-64::mkl-2020.0-166
mkl-service pkgs/main/win-64::mkl-service-2.3.0-py38hb782905_0
mkl_fft pkgs/main/win-64::mkl_fft-1.0.15-py38h14836fe_0
mkl_random pkgs/main/win-64::mkl_random-1.1.0-py38hf9181ef_0
numpy pkgs/main/win-64::numpy-1.18.1-py38h93ca92e_0
numpy-base pkgs/main/win-64::numpy-base-1.18.1-py38hc3f5095_1
pyparsing pkgs/main/noarch::pyparsing-2.4.6-py_0
pyqt pkgs/main/win-64::pyqt-5.9.2-py38ha925a31_4
python-dateutil pkgs/main/noarch::python-dateutil-2.8.1-py_0
qt pkgs/main/win-64::qt-5.9.7-vc14h73c81de_0
sip pkgs/main/win-64::sip-4.19.13-py38ha925a31_0
six pkgs/main/win-64::six-1.14.0-py38_0
tornado pkgs/main/win-64::tornado-6.0.3-py38he774522_3
zlib pkgs/main/win-64::zlib-1.2.11-h62dcd97_3
Proceed ([y]/n)? y
Downloading and Extracting Packages
icu-58.2 | 9.4 MB | ########################################################################## | 100%
pyparsing-2.4.6 | 64 KB | ############################################################################ | 100%
qt-5.9.7 | 72.5 MB | ############################################################################ | 100%
matplotlib-3.1.3 | 22 KB | ############################################################################ | 100%
intel-openmp-2020.0 | 1.5 MB | ############################################################################ | 100%
pyqt-5.9.2 | 3.2 MB | ############################################################################ | 100%
six-1.14.0 | 27 KB | ############################################################################ | 100%
numpy-base-1.18.1 | 3.8 MB | ############################################################################ | 100%
mkl_fft-1.0.15 | 119 KB | ############################################################################ | 100%
jpeg-9b | 245 KB | ############################################################################ | 100%
cycler-0.10.0 | 14 KB | ############################################################################ | 100%
tornado-6.0.3 | 601 KB | ############################################################################ | 100%
blas-1.0 | 6 KB | ############################################################################ | 100%
mkl-service-2.3.0 | 50 KB | ############################################################################ | 100%
sip-4.19.13 | 262 KB | ############################################################################ | 100%
numpy-1.18.1 | 6 KB | ############################################################################ | 100%
python-dateutil-2.8. | 224 KB | ############################################################################ | 100%
freetype-2.9.1 | 450 KB | ############################################################################ | 100%
zlib-1.2.11 | 110 KB | ############################################################################ | 100%
libpng-1.6.37 | 333 KB | ############################################################################ | 100%
mkl-2020.0 | 98.9 MB | ############################################################################ | 100%
mkl_random-1.1.0 | 243 KB | ############################################################################ | 100%
kiwisolver-1.0.1 | 51 KB | ############################################################################ | 100%
matplotlib-base-3.1. | 4.9 MB | ############################################################################ | 100%
icc_rt-2019.0.0 | 6.0 MB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(base-3.8) r:\learn\install>
As you can see, even though we thought we were only installing matplotlib
, conda
also installed all the required dependencies. Certainly makes life easier.
Add Virtual Environment to VS Code Project
Start up VS Code (I used the Startup Menu button/icon I added earlier). Then open the install/play
folder — click the big blue button in the Explorer tab on the left of the VS Code window. The “pages” icon should be highlighted if the Explorer tab is open. If not click the icon.
The tab should now show a header with the folder name and list the test_conda_python.py
file under it. Click the file to open it in an editor window.
Now we want to select a Python interpreter for our project. Press Ctrl+Shift+P
or select ‘View - Command Palette…’ from the menu. In the search box at the top of the new window enter ‘python select’. From the list click ‘Python: Select interpreter’. Find the entry that refers to the virtual environment we created earlier, and select it. Something like ‘Python 3.8.1 64-bit(‘base-3.8’: conda).
A new folder, .vscode
, appeared under the ‘INSTALL’ header.
And, I got a message about no linter being installed, with an offer to install pylint. I said go ahead and install. Then clicked on ‘install with conda". You will see the installation output in the built-in Terminal window at the bottom of the right section of the VS Code window.
If you look at the left side of the information bar at the bottom of the window, you will see it shows the Python interpreter from our virtual environment.
Save Workspace
Now select ‘File - Save Workspace As…’, and enter a name. I chose ‘pyPlay’. If you look in the Explorer tab, you will now, below the ‘Open Editors’ section, that the ‘INSTALL’ header has had a name change. In my case ‘PLYPLAY (WORKSPACE)’.
Ok let’s close the workspace (File - Close Workspace) and VS Code. Then restart VS Code.
On restart I got 2 windows. One with an open ‘Untitled’ workspace. And one with only a ‘Welcome’ window. Don’t understand why at this point, but close the one with the untitled workspace. Select ‘Don’t Save’ when asked if you wish to save the workspace.
New Workspace
In the Welcome window, you will see under ‘Recent’ a link called ‘pyPlay (Workspace)’. Well you may have named it something else. And as I haven’t changed any of the display settings, the link is in Blue against a black background. Sorry, that color theme may have been kept over from my previous installation of VS Code. So, adjust accordingly.
Click the ‘pyPlay (Workspace)’ link. We should be right back to where we left off before closing our new workspace.
Test Code
Now, click on the code file to make it the active editor window. Right click in the editor window and select ‘Run Python File in Terminal’. In the Terminal window (may need to click appropriate tab) you will see that VS Code activated our virtual environment, then ran the python code file. Which then produced the desired output.
All way to easy!
Sorry, but that’s it for this post. I always expect to do more, but somehow don’t get there. Might have something to do with my verbosity and the lack of a solid plan. But, then, we are just learning. And, somewhat by the seat of our pants.
P.S. When I closed the workspace, I noticed a small 1
over the ‘Settings’ icon at the bottom of the icon bar on the left of the VS Code window. (I guess I should really learn the ‘proper’ names for all the various bars, tabs and sections of the VS Code window.) I clicked the icon and among the many items in the menu there was one saying something like ‘install update’. So I clicked on it and replied as necessary to get the update done. VS Code gets fairly regular updates. And, they usually don’t take too long and once completed you will be returned to wherever you were before initiating the update.