How to Install and Run Multiple Python Versions on macOS | pyenv & virtualenv Setup Tutorial

k0nze
25 Sept 202110:48

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

00:00

πŸ’» 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.

05:03

πŸ”§ 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.

10:03

🎬 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

pyenv is a command-line tool that allows users to install and manage multiple versions of Python on the same system. It is central to the video's theme as it enables seamless switching between different Python versions. In the script, pyenv is used to install Python 3.9.7 and the latest release candidate 3.10.0rc2, demonstrating its utility for developers who need to work with multiple Python environments.

πŸ’‘Xcode Command Line Tools

Xcode Command Line Tools are a set of software development tools for macOS that include compilers and other command-line utilities. In the context of the video, these tools are a prerequisite for installing pyenv, as they provide essential components for building and managing Python installations on macOS.

πŸ’‘Brew

Brew, or Homebrew, is a package manager for macOS that simplifies the installation of software. In the video, Brew is used to install libraries such as openssl, readline, sqlite3, xz, and zlib, which are necessary for setting up the environment for pyenv to work properly.

πŸ’‘Virtual Environments

Virtual environments are isolated spaces within a project that allow developers to work with specific versions of libraries and Python itself without affecting the global Python installation. In the script, virtual environments are created using the 'venv' module to manage dependencies for individual projects, showcasing their importance in Python development.

πŸ’‘Python

Python is a high-level, interpreted programming language widely used for web development, data analysis, artificial intelligence, and more. The video focuses on managing different versions of Python, emphasizing its importance as a versatile tool in the developer's toolkit. The script mentions installing and switching between Python 3.9.7 and the 3.10.0 release candidate.

πŸ’‘Dependencies

Dependencies in the context of Python projects refer to libraries or modules that a project requires to function properly. The video discusses managing dependencies within virtual environments to avoid conflicts between projects, which is crucial for maintaining a stable development environment.

πŸ’‘Release Candidate

A release candidate is a version of software that is almost ready for release but is still undergoing testing. In the script, the latest Python release candidate, 3.10.0rc2, is installed and tested using pyenv, illustrating how developers can try out new versions before they are officially released.

πŸ’‘Visual Studio Code

Visual Studio Code is a popular source code editor that supports Python development among other languages. The video demonstrates how to configure Visual Studio Code to work with pyenv and virtual environments, showing its integration capabilities with development tools.

πŸ’‘Environment Variables

Environment variables are a set of dynamic values in the operating system that can affect the way running processes behave. In the script, environment variables are set for pyenv to ensure that the correct Python version is used when the terminal is opened, highlighting their role in configuring the development environment.

πŸ’‘Pip

Pip is a package installer for Python that allows users to install and manage Python packages. In the video, pip is used within virtual environments to install project-specific modules without affecting the global Python installation, demonstrating the importance of pip in Python package management.

πŸ’‘Settings JSON

Settings JSON is a configuration file used in Visual Studio Code to customize editor behavior and settings for a specific workspace. In the script, a settings JSON is created to ensure that the virtual environment is automatically activated when working on a project in Visual Studio Code, showing how developers can tailor their development environment to their needs.

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

play00:00

you want to install different python

play00:01

versions under mac os because you work

play00:03

on different python projects that

play00:04

require different python versions or

play00:06

different python dependencies or you

play00:08

just want to check out the latest python

play00:10

release candidate in this video i'm

play00:12

going to show you how to install and use

play00:14

different python versions using pyenv

play00:17

together with virtual environments and

play00:18

how to use those with visual studio code

play00:21

welcome i'm cons i got a master's degree

play00:23

in computer science and currently work

play00:25

as a graduate research assistant in

play00:26

computer science and work with python on

play00:29

a daily basis which makes python to one

play00:31

of my most important tools installing

play00:33

and using different python versions

play00:35

under mac os is actually pretty easy

play00:36

using pyen pinef is a command line tool

play00:38

which allows you to seamlessly switch

play00:40

between different python versions and

play00:42

also enables you to set specific python

play00:44

versions for dedicated folders together

play00:47

with virtual environments it's an

play00:49

incredibly useful tool for everyone who

play00:51

works with python and without further

play00:53

ado let's jump right into mac os to set

play00:55

everything up before we can start to

play00:57

install pi and we have to install other

play00:59

software packages the first software

play01:01

package we have to install are the xcode

play01:03

command line tools for which we are

play01:05

going to use this command x code minus

play01:06

select minus minus install and you can

play01:08

find all the commands

play01:10

used in this video on my website for

play01:12

which you can find the link down below

play01:14

in the description

play01:15

so we're going to copy this xcode

play01:17

command over here and just paste it in

play01:19

the terminal and press enter and i

play01:21

already installed the xcode command line

play01:23

tools before

play01:24

now we have to install several libraries

play01:27

which will be provided by brew we're

play01:29

going to install openssl readline sql3

play01:33

xz and zlip and we're going to copy that

play01:36

as well and enter that into our command

play01:40

line and press enter with those packages

play01:42

installed we can start installing pine

play01:44

if there are actually two ways to

play01:46

install pine the first one is to use

play01:48

brew and the second one is to install pi

play01:50

f directly from github and i'm going to

play01:52

use the second one because i want a

play01:55

little bit more control over my pie and

play01:57

environment but you can also use the

play01:59

brew command and then you just enter

play02:02

brew install pie you can also find that

play02:04

on my website so we scroll down and

play02:07

we're just going to clone the github

play02:09

repository payment

play02:11

into our home directory

play02:14

under the

play02:15

directory.pinf and we paste that in our

play02:18

command line and press enter and we've

play02:20

cloning the github repository pinef is

play02:22

technically already installed in our

play02:24

system but we have to set it up now

play02:27

and for that we're going to scroll

play02:29

further down on my website and we're

play02:30

going to enter those three commands

play02:33

in the terminal as well the first one

play02:35

will set an environment variable called

play02:37

pi and root which will point to the pi

play02:41

inf directory we just created which is

play02:43

under home.pion the second one will add

play02:46

the bin directory included in the pi

play02:49

root directory into the path so when we

play02:51

are going to use the pine command in our

play02:53

terminal it is actually found

play02:56

and the last one is the pine init minus

play02:59

minus path and that will take care of

play03:02

initializing pi and every time we open a

play03:04

new terminal so we're just going to copy

play03:07

all three

play03:09

copy and

play03:10

enter them as well into our terminal and

play03:13

press on enter this concludes the

play03:15

installation of pine under macos and we

play03:17

are now going to use it and if you

play03:19

enjoyed the video so far make sure to

play03:21

give it a like to use pine we're going

play03:23

to start a new terminal such that the

play03:24

environment variables are set and the

play03:27

init command is run and now we can enter

play03:30

pi

play03:31

and we are going to be presented with

play03:33

the list of all commands that pions

play03:36

offers the first command we are going to

play03:38

issue is the pine install minus l

play03:40

command this will list all the available

play03:42

python versions that can be installed

play03:44

using pyenv we're going to enter pine

play03:47

install minus l

play03:48

and this will print out a long long list

play03:51

of python versions we can install we

play03:53

have down here python stackless pi pi

play03:57

miniconda

play03:59

jyphon but we are actually interested in

play04:02

the normal c python implementation

play04:04

and the latest version available at

play04:07

the time of recording this video is the

play04:09

version 3.9.7 and we're just going to

play04:12

copy this string and jump back down and

play04:15

we enter

play04:16

pi and install

play04:19

and paste the version string

play04:21

3.9.7 which will install the python

play04:24

version 3.9.7 through pi inf now that

play04:27

the installation of the python version

play04:29

3.9.7 is finished we can enter pi inf

play04:33

versions

play04:34

which will list all the python versions

play04:37

that are currently available through

play04:38

paying we can see we have the system

play04:40

version which comes with the operating

play04:42

system and we have the newly installed

play04:44

version 3.9.7

play04:46

and the currently active version is

play04:49

indicated by this asterisk in front

play04:51

and we would like to set the version

play04:53

3.9.7 for the whole system so we are

play04:56

going to enter pi n

play04:59

global 3.9.7

play05:02

and this will set this python version

play05:05

for every terminal we are going to open

play05:07

so we open a new terminal

play05:09

and in this terminal we're going to

play05:10

enter

play05:12

python

play05:14

minus v

play05:15

and we can see we're actually running

play05:17

python version 3.9.7

play05:20

and i also prepared a little hello world

play05:22

command which prints out the python

play05:24

version we can check that as well that

play05:25

is under

play05:27

python test and when we run it with

play05:30

python

play05:31

hello pi we can see it prints out hello

play05:34

i'm python version 3.9.7 however i would

play05:38

like to check out the latest release

play05:40

candidate of the next python version

play05:41

with my current project

play05:43

for that i'm going to

play05:45

enter pi and

play05:47

install minus l once again

play05:49

and then we're going to check

play05:52

which is the latest release candidate of

play05:54

python

play05:56

and

play05:57

at the time of recording it is 3.10

play06:01

0rc2 i'm going to copy that and enter

play06:06

pi and

play06:08

install and paste that string and press

play06:11

on enter once again which will install

play06:13

the latest release candidate of this

play06:15

python version through pyenv now that

play06:17

the installation of the latest python

play06:19

release candidate is finished we enter

play06:21

pi in

play06:22

versions once again and we can see is

play06:25

actually available

play06:26

i don't want to use this release connect

play06:29

on my whole system i just want to use it

play06:30

for this project and for that i'm going

play06:32

to use the command pi and

play06:35

local and i'm going to enter 3.10.0rc2

play06:41

and this will create a dot python

play06:43

version file when we have a look here

play06:46

it created the dot python version file

play06:49

and when we

play06:51

have a look into that file

play06:53

it includes the version string of this

play06:56

particular python version

play06:57

and if we now

play06:59

enter python

play07:00

minus v we can see we're actually

play07:02

running this python version

play07:05

and we can also check that with our

play07:07

hello world

play07:09

we enter hello pi

play07:11

and we see hello i'm python version

play07:13

3.10.0

play07:16

rc2 usually your python projects depend

play07:19

on a lot of different modules

play07:21

and sometimes those modules have to be

play07:23

at a certain version number and if i

play07:25

install a new module through pip it will

play07:28

install this module system-wide for the

play07:30

currently activated python version for

play07:32

paying

play07:33

however if i only want to install a

play07:35

module for a specific project i'm going

play07:38

to use virtual environments to create a

play07:40

virtual environment for my project i

play07:42

first have to head into the project

play07:44

directory and inside the project

play07:46

directory i'm going to enter python

play07:48

minus m

play07:49

vent

play07:50

dot then

play07:52

which will create a dot went directory

play07:55

which will

play07:56

hold the python interpreter and all the

play07:59

modules that i will be installing

play08:01

through pip and we have a look into the

play08:04

vent directory with ls

play08:06

vent

play08:07

bin we can see it actually has its own

play08:09

python interpreter and its own version

play08:11

of pip in here and that is the version

play08:13

we have set with pyenv to activate our

play08:17

virtual environment we are going to

play08:19

enter

play08:20

source

play08:22

vent

play08:23

bin

play08:24

activate and when it's active we have

play08:27

this dot vent in front of our prompt and

play08:30

we can now just use the normal python

play08:32

interpreter minus v and if we have a

play08:35

look which interpreter is currently used

play08:38

seen with which python we see it

play08:41

actually uses the interpreter from our

play08:43

virtual environment and now i would like

play08:45

to use this virtual environment together

play08:47

with the python version we have set for

play08:49

pyth in visual studio code for that i'm

play08:52

going to open visual studio code within

play08:55

my project directory

play08:56

i just enter code dot and that is will

play08:59

open

play09:00

virtual visual studio code in this

play09:03

directory and we can see we have this

play09:05

hello pi which just prints out the

play09:07

python version

play09:08

to tell visual studio code to always

play09:11

activate the virtual environment when we

play09:13

enter this project we have to add a

play09:16

settings json to our project for that

play09:18

i'm going to enter command shift p to

play09:21

bring up the command palette and then

play09:23

enter

play09:24

settings

play09:26

json and we're going to open the

play09:28

settings json for this workspace

play09:32

it will open a empty json file and in

play09:34

this json file i'm going to enter

play09:37

python

play09:39

terminal

play09:41

activate environment true

play09:43

i'm going to save that

play09:47

close the settings json

play09:49

and if we now go to

play09:51

run

play09:52

run without debugging

play09:55

a new terminal opens up and we can see

play09:57

it automatically activates a virtual

play09:59

environment we see we have the event in

play10:02

the front

play10:03

and when we run our little hello world

play10:06

script we can see it says hello i'm

play10:08

python version 3.10 point rc2 this

play10:12

concludes this video on how to install

play10:14

different python versions using pi inf

play10:16

and how to use virtual environments

play10:18

together with visual studio code and

play10:20

pyenv i hope you enjoyed this video let

play10:23

me know down below in the comments if it

play10:25

was helpful for you and remember that

play10:27

you can find all the commands used in

play10:29

this video on my website for which you

play10:30

can find the link down below in the

play10:32

description

play10:34

remember to give this video a like and

play10:36

if you want to catch up with me join our

play10:38

lovely discord community last but not

play10:40

least big shout out to my patreon

play10:42

subscribers for their support i hope to

play10:44

see you in my next video and bye bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
PythonpyenvmacOSDevelopmentVirtual EnvironmentsCodingProjectsXcodeBrewEducationalTutorial