mkdir istec14 Virtual Environments
14.1 Task : Manage Python Virtual Environments
These solutions are assuming the usage of macOS and Visual Studio Code
14.1.1 Virtual Environment 1
Create a folder for this work called istec and move into it. Make sure you know where you are positioned.
- Positioned in folder istec
- Create a Python environment named env1
python -m venv env1- Activate the environment named env1
source env1/bin/activate- Install package requests version 2.18.4 in this environment
pip install requests==2.18.4It’s possible that you have a message saying to consider to upgrade pip. You may do it or not.
- List the existing packages in this environment
pip listYou may have a similar output
Package Version
---------- ---------
certifi 2022.12.7
chardet 3.0.4
idna 2.6
pip 23.0.1
requests 2.18.4
setuptools 58.1.0
urllib3 1.22Confirm the package requests version 2.18.4 is installed. Some other packages were installed because requests needs them.
Open you Visual Studio Code editor and open folder istec. It should have only env1.
Visual Studio Code may detect you have a virtual environment already there.
Select that first environment env1.
Create a new file as a Notebook (*.ipynb) called FirstNotebook.ipynb in this environment
- Add in the Notebook, as the first block, the following Markdown
# My First NotebookAdd as the next block, the following Code
print("I now know about Markdown & Code blocks.")
print("I also created my first Python environment.")- Execute the Notebook
- If needed, select a Python Kernel and Environment (env1) to execute the Notebook
- Install any software that Visual Studio Code asks you
14.1.2 Virtual Environment 2
- Positioned in folder istec
- Create a Python environment named env2
python -m venv env2- Activate the environment named env2
source env2/bin/activate- Install package requests version 2.18.4 in this environment
pip install requests==2.18.4It’s possible that you have a message saying to consider to upgrade pip. You may do it or not.
- List the existing packages in this environment
pip listYou may have a similar output
Package Version
---------- ---------
certifi 2022.12.7
chardet 3.0.4
idna 2.6
pip 23.0.1
requests 2.18.4
setuptools 58.1.0
urllib3 1.22Confirm the package requests version 2.18.4 is installed. Some other packages were installed because requests needs them.
- In Visual Studio Code editor open folder istec.
- Now, when selecting a new Kernel, you should have env1 and env2 to select.
14.1.3 Virtual Environment 3
- Positioned in folder istec
- Create a Python environment named env3
python -m venv env3- Activate the environment named env3
source env3/bin/activate- Install package requests with the latest version in this environment
'''
Note the omission of a version.
It's installing the latest available.
'''
pip install requests It’s possible that you have a message saying to consider to upgrade pip. You may do it or not.
- List the existing packages in this environment
pip listYou may have a similar output
Package Version
---------- ---------
certifi 2022.12.7
charset-normalizer 3.1.0
idna 3.4
pip 23.0.1
requests 2.28.2
setuptools 58.1.0
urllib3 1.26.15Confirm the package requests version is different from 2.18.4. Some other packages were installed because requests needs them.
- In Visual Studio Code editor open folder istec.
- Now, when selecting a new Kernel, you should have env1, env2 and env3 to select.