How to Install and Run Multiple Python Versions on macOS | pyenv & virtualenv Setup Tutorial
Summary
TLDRThis tutorial demonstrates how to manage multiple Python versions on macOS using pyenv, a command-line tool. It covers installation of pyenv alongside necessary libraries, setting up the environment, and switching between Python versions. The video also explains how to utilize virtual environments for projects with specific dependencies and integrates this setup with Visual Studio Code for a seamless development experience.
Takeaways
- 😀 The video demonstrates how to install and use different Python versions on macOS using pyenv.
- 🛠️ The first step is to install the Xcode Command Line Tools using a specific command in the terminal.
- 📚 Several libraries, including openssl, readline, sqlite3, xz, and zlib, are required to be installed via Homebrew before pyenv.
- 🔧 Pyenv can be installed either via Homebrew or by cloning the GitHub repository, with the latter offering more control over the environment.
- 📝 After installing pyenv, specific commands are needed to set up environment variables and initialize pyenv for every new terminal session.
- 🔍 Pyenv allows listing all available Python versions with the 'pyenv install -l' command.
- 🔄 It enables the installation of specific Python versions, such as 3.9.7 or release candidates like 3.10.0rc2.
- 🌐 Pyenv can set a specific Python version globally or locally for dedicated projects.
- 📁 For project-specific dependencies, virtual environments can be created with 'python -m venv .venv'.
- 💻 To integrate with Visual Studio Code, a settings.json file can be configured to automatically activate the virtual environment when the project is opened.
- 🔗 All commands and additional resources can be found on the presenter's website, with a link provided in the video description.
Q & A
What is the purpose of using pyenv on macOS?
-Pyenv is used to install and manage multiple Python versions on macOS, allowing developers to switch between different Python versions and set specific versions for dedicated projects or to check out the latest release candidates.
Why is it necessary to install the Xcode Command Line Tools before installing pyenv?
-The Xcode Command Line Tools are required for the installation of other software packages that pyenv depends on, such as libraries for OpenSSL, readline, sqlite3, xz, and zlib.
How can you install the Xcode Command Line Tools on macOS?
-You can install the Xcode Command Line Tools by running the command 'xcode-select --install' in the terminal.
What are the steps to install pyenv using the GitHub repository instead of Homebrew?
-To install pyenv from GitHub, you first clone the pyenv repository into your home directory under the .pyenv directory, and then set up environment variables and initialize pyenv by entering specific commands in the terminal.
How do you list all the available Python versions that can be installed using pyenv?
-You can list all available Python versions by using the command 'pyenv install -l' in the terminal.
What command is used to install a specific Python version using pyenv?
-The command to install a specific Python version is 'pyenv install <version>', where <version> is the version string of the Python version you want to install.
How can you set a specific Python version to be used globally for all terminal sessions?
-You can set a global Python version by using the command 'pyenv global <version>', where <version> is the version string of the Python version you want to set as the default.
What is the purpose of creating a .python-version file in a project directory?
-The .python-version file specifies the Python version to be used for that particular project, ensuring that the project runs with the correct Python version set by pyenv.
How do you create and activate a virtual environment for a Python project?
-You create a virtual environment by navigating to the project directory and running 'python -m venv .venv', and then activate it by running 'source .venv/bin/activate'.
How can you configure Visual Studio Code to automatically activate the virtual environment when working on a specific project?
-You can configure Visual Studio Code to activate the virtual environment by adding "python.pythonTerminal.activateEnvironment": true to the project's settings.json file.
What is the benefit of using virtual environments with pyenv and Visual Studio Code?
-Using virtual environments with pyenv and Visual Studio Code allows developers to isolate project dependencies, ensuring that each project has its own specific Python version and installed modules without affecting other projects.
Outlines
💻 Installing Multiple Python Versions with pyenv on macOS
This paragraph introduces the video's focus on using pyenv to manage different Python versions on macOS, which is essential for developers working on multiple projects with varying Python requirements. The speaker, Cons, a computer science graduate and research assistant, explains the process of installing pyenv alongside virtual environments to facilitate seamless version switching. The video begins with the installation of the Xcode command line tools and necessary libraries using Homebrew, followed by the installation of pyenv itself, either via Homebrew or directly from GitHub. The speaker opts for the GitHub method for greater control over the installation. After setting up the environment variables and initialization, the viewer is guided through listing available Python versions and installing a specific version, such as 3.9.7, using pyenv.
🔧 Setting Python Versions and Using Virtual Environments
The second paragraph delves into setting a specific Python version for the entire system using pyenv's global command and verifying the installation with version-specific scripts. It also covers the process of installing a Python release candidate, such as 3.10.0rc2, for testing purposes. The speaker demonstrates how to create and activate a virtual environment within a project directory using Python's built-in 'venv' module. This allows for project-specific dependencies without affecting the system-wide Python installation. The paragraph concludes with integrating the virtual environment with Visual Studio Code, ensuring the IDE activates the correct environment and interpreter for the project, as detailed in a settings.json configuration.
🎬 Conclusion and Additional Resources
The final paragraph wraps up the video with a summary of the steps covered for installing different Python versions using pyenv and utilizing virtual environments with Visual Studio Code. The speaker invites viewers to share their feedback in the comments and provides a link to their website for access to all commands used in the video. There is also an invitation to join a Discord community and a special thanks to Patreon supporters. The video concludes with a farewell and an anticipation for the next video, emphasizing the value of the presented information and the support from the community.
Mindmap
Keywords
💡pyenv
💡Xcode Command Line Tools
💡Brew
💡Virtual Environments
💡Python
💡Dependencies
💡Release Candidate
💡Visual Studio Code
💡Environment Variables
💡Pip
💡Settings JSON
Highlights
Installing different Python versions on macOS using pyenv for projects with varying requirements.
Using pyenv with virtual environments and Visual Studio Code for seamless Python development.
Installing Xcode command line tools as a prerequisite for pyenv setup.
Installing necessary libraries like openssl, readline, sql3, xz, and zlip via brew.
Two methods for installing pyenv: using brew or cloning from GitHub.
Setting up environment variables and initializing pyenv after installation.
Listing available Python versions with pyenv install -l.
Installing a specific Python version using pyenv install <version>.
Setting a global Python version for all terminals with pyenv global <version>.
Checking the active Python version with python -v.
Installing the latest Python release candidate for project-specific needs.
Using pyenv local to set a Python version for a specific project directory.
Creating and managing virtual environments with Python's built-in venv module.
Activating virtual environments to isolate project dependencies.
Configuring Visual Studio Code to automatically activate virtual environments.
Using a settings.json file in VS Code to ensure virtual environment activation.
Running Python scripts in VS Code with the correct Python version and virtual environment.
Finding all commands used in the video on the presenter's website.
Invitation to join the presenter's Discord community and support on Patreon.
Transcripts
you want to install different python
versions under mac os because you work
on different python projects that
require different python versions or
different python dependencies or you
just want to check out the latest python
release candidate in this video i'm
going to show you how to install and use
different python versions using pyenv
together with virtual environments and
how to use those with visual studio code
welcome i'm cons i got a master's degree
in computer science and currently work
as a graduate research assistant in
computer science and work with python on
a daily basis which makes python to one
of my most important tools installing
and using different python versions
under mac os is actually pretty easy
using pyen pinef is a command line tool
which allows you to seamlessly switch
between different python versions and
also enables you to set specific python
versions for dedicated folders together
with virtual environments it's an
incredibly useful tool for everyone who
works with python and without further
ado let's jump right into mac os to set
everything up before we can start to
install pi and we have to install other
software packages the first software
package we have to install are the xcode
command line tools for which we are
going to use this command x code minus
select minus minus install and you can
find all the commands
used in this video on my website for
which you can find the link down below
in the description
so we're going to copy this xcode
command over here and just paste it in
the terminal and press enter and i
already installed the xcode command line
tools before
now we have to install several libraries
which will be provided by brew we're
going to install openssl readline sql3
xz and zlip and we're going to copy that
as well and enter that into our command
line and press enter with those packages
installed we can start installing pine
if there are actually two ways to
install pine the first one is to use
brew and the second one is to install pi
f directly from github and i'm going to
use the second one because i want a
little bit more control over my pie and
environment but you can also use the
brew command and then you just enter
brew install pie you can also find that
on my website so we scroll down and
we're just going to clone the github
repository payment
into our home directory
under the
directory.pinf and we paste that in our
command line and press enter and we've
cloning the github repository pinef is
technically already installed in our
system but we have to set it up now
and for that we're going to scroll
further down on my website and we're
going to enter those three commands
in the terminal as well the first one
will set an environment variable called
pi and root which will point to the pi
inf directory we just created which is
under home.pion the second one will add
the bin directory included in the pi
root directory into the path so when we
are going to use the pine command in our
terminal it is actually found
and the last one is the pine init minus
minus path and that will take care of
initializing pi and every time we open a
new terminal so we're just going to copy
all three
copy and
enter them as well into our terminal and
press on enter this concludes the
installation of pine under macos and we
are now going to use it and if you
enjoyed the video so far make sure to
give it a like to use pine we're going
to start a new terminal such that the
environment variables are set and the
init command is run and now we can enter
pi
and we are going to be presented with
the list of all commands that pions
offers the first command we are going to
issue is the pine install minus l
command this will list all the available
python versions that can be installed
using pyenv we're going to enter pine
install minus l
and this will print out a long long list
of python versions we can install we
have down here python stackless pi pi
miniconda
jyphon but we are actually interested in
the normal c python implementation
and the latest version available at
the time of recording this video is the
version 3.9.7 and we're just going to
copy this string and jump back down and
we enter
pi and install
and paste the version string
3.9.7 which will install the python
version 3.9.7 through pi inf now that
the installation of the python version
3.9.7 is finished we can enter pi inf
versions
which will list all the python versions
that are currently available through
paying we can see we have the system
version which comes with the operating
system and we have the newly installed
version 3.9.7
and the currently active version is
indicated by this asterisk in front
and we would like to set the version
3.9.7 for the whole system so we are
going to enter pi n
global 3.9.7
and this will set this python version
for every terminal we are going to open
so we open a new terminal
and in this terminal we're going to
enter
python
minus v
and we can see we're actually running
python version 3.9.7
and i also prepared a little hello world
command which prints out the python
version we can check that as well that
is under
python test and when we run it with
python
hello pi we can see it prints out hello
i'm python version 3.9.7 however i would
like to check out the latest release
candidate of the next python version
with my current project
for that i'm going to
enter pi and
install minus l once again
and then we're going to check
which is the latest release candidate of
python
and
at the time of recording it is 3.10
0rc2 i'm going to copy that and enter
pi and
install and paste that string and press
on enter once again which will install
the latest release candidate of this
python version through pyenv now that
the installation of the latest python
release candidate is finished we enter
pi in
versions once again and we can see is
actually available
i don't want to use this release connect
on my whole system i just want to use it
for this project and for that i'm going
to use the command pi and
local and i'm going to enter 3.10.0rc2
and this will create a dot python
version file when we have a look here
it created the dot python version file
and when we
have a look into that file
it includes the version string of this
particular python version
and if we now
enter python
minus v we can see we're actually
running this python version
and we can also check that with our
hello world
we enter hello pi
and we see hello i'm python version
3.10.0
rc2 usually your python projects depend
on a lot of different modules
and sometimes those modules have to be
at a certain version number and if i
install a new module through pip it will
install this module system-wide for the
currently activated python version for
paying
however if i only want to install a
module for a specific project i'm going
to use virtual environments to create a
virtual environment for my project i
first have to head into the project
directory and inside the project
directory i'm going to enter python
minus m
vent
dot then
which will create a dot went directory
which will
hold the python interpreter and all the
modules that i will be installing
through pip and we have a look into the
vent directory with ls
vent
bin we can see it actually has its own
python interpreter and its own version
of pip in here and that is the version
we have set with pyenv to activate our
virtual environment we are going to
enter
source
vent
bin
activate and when it's active we have
this dot vent in front of our prompt and
we can now just use the normal python
interpreter minus v and if we have a
look which interpreter is currently used
seen with which python we see it
actually uses the interpreter from our
virtual environment and now i would like
to use this virtual environment together
with the python version we have set for
pyth in visual studio code for that i'm
going to open visual studio code within
my project directory
i just enter code dot and that is will
open
virtual visual studio code in this
directory and we can see we have this
hello pi which just prints out the
python version
to tell visual studio code to always
activate the virtual environment when we
enter this project we have to add a
settings json to our project for that
i'm going to enter command shift p to
bring up the command palette and then
enter
settings
json and we're going to open the
settings json for this workspace
it will open a empty json file and in
this json file i'm going to enter
python
terminal
activate environment true
i'm going to save that
close the settings json
and if we now go to
run
run without debugging
a new terminal opens up and we can see
it automatically activates a virtual
environment we see we have the event in
the front
and when we run our little hello world
script we can see it says hello i'm
python version 3.10 point rc2 this
concludes this video on how to install
different python versions using pi inf
and how to use virtual environments
together with visual studio code and
pyenv i hope you enjoyed this video let
me know down below in the comments if it
was helpful for you and remember that
you can find all the commands used in
this video on my website for which you
can find the link down below in the
description
remember to give this video a like and
if you want to catch up with me join our
lovely discord community last but not
least big shout out to my patreon
subscribers for their support i hope to
see you in my next video and bye bye
関連動画をさらに表示
How to set up Python on Visual Studio Code
How to set up Visual Studio Code for Executing and Debugging C Programs | Tutorial
#2 Python Tutorial for Beginners | Python Installation | PyCharm
YOLOv7 | Instance Segmentation on Custom Dataset
Python Tutorial for Beginners 1: Install and Setup for Mac and Windows
BELAJAR FLUTTER DARI NOL - SECTION 1 - 01 Introduction
5.0 / 5 (0 votes)