Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Installing Python Packages Into a Virtual Environment

This lesson covers how to download and install third party Python packages into a virtual environment using the pip tool. You’ll also see the correct way to use pip while the virtual environment is active.

Avatar image for Omer Faruk

Omer Faruk on April 20, 2019

Hi, Thanks for the nice video. Could you please show how we can initialize git repository while the virtual environment i active.

Avatar image for Jim Anderson

Jim Anderson RP Team on April 20, 2019

Hi, Omer! git is largely unaware of Python virtualenvs. (Unless you’re calling git from python directly, but that doesn’t sound like what you’re doing :) )

If you need a git refresher, the commands for initializing and using git are covered in our Intro to Git article

Avatar image for Omer Faruk

Omer Faruk on April 20, 2019

Thank you Jim. I am using some git commnds but I did not use them with virtual environment. For instance, I have a simple Python project:

HelloWorld — venv — bin — include — lib — pyenv.cfg — example1.py — example2.py

In order to add this project to version control, I am planning to follow the steps below. I am not sure whether this is a correct way. I also wonder best practices. 1- cd HelloWorld 2- init git repository 3- add venv directory into .gitignore file

Avatar image for Jim Anderson

Jim Anderson RP Team on April 20, 2019

That sounds like a good approach. You don’t want the venv in your repo. You should think about a requirements.txt file to store what dependencies you have and THAT can be in the repo.

Hope that helps!

Avatar image for rehdwolfe

rehdwolfe on July 5, 2019

Do you happen to have a link handy for using requirements.txt?

Avatar image for Dan Bader

Dan Bader RP Team on July 5, 2019

Avatar image for shevachp

shevachp on March 15, 2020

Thanks. Very very (not a type) helpful. I would like to know how can I code in IDLE in the virtual enviroment. Thanks

Avatar image for Ricky White

Ricky White RP Team on March 16, 2020

Hi shevachp. IDLE is not made for writing production code in and therefor not for writing apps in virtual environments. I would recommend you upgrade to a more extensive code editor or IDE. We have a guide on IDEs and editors here: realpython.com/python-ides-code-editors-guide/

Hope that helps.

Would it be possible to update this video series to be friendly to users on Windows using powershell because it is very different?

Avatar image for jeanbai

jeanbai on Nov. 12, 2020

Hello,

I am watching the section of “Installing python package into a virtual environment”. At the time of “01:01” how did you put the command (venv) rorschach:my-python-project daniel$ back in a sudden?

Please help.

Thanks

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Nov. 12, 2020

@jeanbai You can exit the Python interpreter by sending the end-of-file (EOF) character to it. It’s associated with different keyboard shortcuts on different operating systems. On Linux, for example, you can hit [Ctrl] + [D], while on Windows it’s [Ctrl] + [Z] followed by [Enter].

Avatar image for gregorysaison

gregorysaison on Nov. 12, 2020

@WD I’m using Powershell too and I was blocked by the command to enter for this case. After a few research I found my solution:

In Powershell on Windows, the command is a little bit different:

environment-name\\Scripts\\Activate.ps1
Avatar image for Thomas

Thomas on May 16, 2021

Hello,

I’ve created a virtual environment and activated the environment. I was trying to test if some code was able to run before I installed the appropriate package:

from urllib.request import urlopen

url = "http://olympus.realpython.org/profiles/dionysus"
page = urlopen(url)
html = page.read().decode("utf-8")
print(html)

However, since I have not installed the requests or urllib package in the virtual environment, it still succeeds in the running the code above without any errors. I have a couple of questions:

  1. How can that happen? I assume for running the urlopen module the packages should first be installed in the virtual environment? Or does every virtual environment come with a standard set of packages?
  2. When a virtual environment is active and it does not find the right packages, does it automatically search for those packages in the global environment?
  3. Does it matter where exactly you save your scripts and how you run them? E.g. from terminal or using IDLE?
Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on May 16, 2021

@Thomas It works because urllib comes preinstalled with Python. It’s always available through the standard library, even in your virtual environments.

The idea behind virtual environments is to have isolated Python interpreters, so when a module can’t be found in your virtual environment, then Python won’t automatically search for it elsewhere.

It does matter which Python binary you run the script through. That will effectively determine which virtual environment and dependencies will be used.

Avatar image for saddlemeyer

saddlemeyer on April 18, 2022

Hi! I’ve followed along closely up until this point but am getting an error when I try to import the schedule module:

(venv)   Test_Project python
Python 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import schedule
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'schedule'
>>>

Any idea what I could be doing wrong?

Avatar image for Dan Bader

Dan Bader RP Team on April 19, 2022

Hey @saddlemeyer, could you post a longer console session transcript that shows the commands before you launch the Python interpreter, including the commands for creating the virtual environment and installing schedule. That extra context will be useful for figuring out what happened there :)

Become a Member to join the conversation.