Isolating Dependencies With Virtual Environments
00:00 In this part of the course, you’ll learn how to isolate your Python dependencies using so called virtual environments. Here is where you are right now in the course.
00:11 And this is what you are going to cover in the upcoming module in the course. You are going to learn about virtual environments, what they are, how they work, and what they are good for.
00:23 Next, you are going to learn how to create and activate a virtual environment on your system, after that, you’re going to learn how to install packages into a virtual environment; you’ll also learn how to deactivate virtual environments and how to completely delete them again.
00:39 And last, I am going to show you some tips in my personal workflow that I use to make working with virtual environments a little bit more efficient. Okay, ready? Let’s jump right in!
00:49 So we just learned that Python has a powerful package management system. What do we need that isolation for? The thing is that by default, pip installs all packages in a single shared environment, which means that if you need different versions of Python packages, there are going to be version conflicts.
01:08 Here is an example. Imagine you are working on two projects. Project #1 needs a specific version of Django, let’s say version 1.8, and it also needs a specific version of the Requests library, let’s say version 2.0.
01:22 Now, the other project, Project #2, needs a completely different version of Django, and it also needs a different version of Requests. Now if all of your packages need to be installed into the same shared environment, there are going to be version conflicts between these packages.
01:40 Another kind of version conflict you might face is that let’s say you are working on one project that actually needs Python 2.7 to run, and all of your new development happens in Python 3.
01:52 So, maybe you’re working on another project that actually requires Python 3.6 to run. How do you resolve these version conflicts? Virtual environments are really helpful tool that can help you with this problem, so a virtual environment allows you to isolate all of your Python dependencies by project, and this works for packages and also for different versions of the Python interpreter.
02:18 So you can think of these virtual environments as individual sandboxes for each project.
Dan Bader RP Team on Sept. 17, 2019
Hi aravind, check out these three courses:
- Test-Driven Development With PyTest
- Writing Cleaner Python Code With PyLint
- Continuous Integration With Python
I also recommend that you take a look at this learning path:
aravind on Sept. 18, 2019
Hi Dan, thanks. I will go through the links.
Meanwhile, do you have a video or upcoming course on “TOX”? I think its tox.readthedocs.io/en/latest/. * Tox vs. PyTest * When to use which * Pros/Cons of each * Sample test cases using Tox
Dan Bader RP Team on Sept. 18, 2019
Yep, we do have a Python testing tutorial on tox. pytest
and tox
do two completely different things, so it’s not really a “pytest vs tox” question IMO. But you’ll hear all about it in the tutorial I linked above :)
aravind on Sept. 19, 2019
Fantastic, Dan - really loving the courses on this website and also the support in the comments/forums - keep up the great work & looking forward to purchasing further courses!
Bobby Tagget on Oct. 28, 2023
Why can’t we just always make sure we are keeping our software versions up to date instead of using virtual environments?
The only reason I can think of, is that, we can’t be sure that the latest version of a package won’t break our software.
I would really appreciate input into this because I have very little confidence in the efficiency of virtual environments.
Bartosz Zaczyński RP Team on Oct. 30, 2023
@Bobby Tagget When you work on a single hobby project where you’re the only developer, then using virtual environments can indeed seem redundant because you generally want to use the latest versions of your dependencies. However, virtual environments help solve problems that may arise when you work on multiple projects simultaneously or if you’re a member of a larger team.
More specifically, different projects often depend on different versions of the same library. Without isolated virtual environments per project, this could lead to version conflicts, informally known as dependency hell. The larger your project, the bigger of a problem this becomes because it’s more likely to have a conflict.
Other than that, virtual environments ensure reproducibility, which helps other team members work on exactly the same library versions as yours. When deploying your project to a remote environment, you also want it to use the expected library versions as opposed to whatever comes installed on that machine.
dakshnavenki on Dec. 1, 2024
Hello, I am using Visual Source code (VSC) for my Pythond development project. Is that comes with virtual environment or do we need to install specific applications to create and use virtual environment for python projects?
Thanks, Venkat
dakshnavenki on Dec. 1, 2024
@Dan, @Bartosz, While working in corporate environments, we are mostly under proxy network and while in proxy, it becomes hard to install new packages using pip. Do we have any good course or tutorial on how to install packages under proxy network? This would be of great help.
Thanks,
Venkat
Bartosz Zaczyński RP Team on Dec. 2, 2024
@dakshnavenki The proxy server is likely there for a reason, in order to prevent employees from installing software from unauthorized sources. To bypass this restriction, some development teams set up a local PyPI server that proxies requests to the official one. Additionally, it can host proprietary modules that should never be published to the official PyPI, e.g., to avoid accidental leaks.
However, to route traffic through your company’s proxy server, you can use the --proxy
option of the pip
command, as shown below:
$ python -m pip install requests --proxy http://proxy.server:1234
If the proxy requires authentication, then you can optionally include your credentials:
$ python -m pip install requests --proxy http://user:password@proxy.server:1234
Let us know if that helps!
dakshnavenki on Dec. 2, 2024
Thanks for the answer Bartosz, but I’m not able to get it worked. I’ve given the response on pip install
command below, both with and without --proxy
option. Please correct me where I am going wrong and what’s the exact issue.
Without proxy option:
C:\Users\vdhatchanamoorthy>pip install requests
*Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://repos.proxy.l-5com.com/artifactory/api/pypi/pypi/simple
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by *'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x00000239A35F9FC0>, 'Connection to repos.proxy.l-3com.com timed out. (connect timeout=15)')': /artifactory/api/pypi/pypi/simple/requests/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x00000239A35FA2C0>, 'Connection to repos.proxy.l-3com.com timed out. (connect timeout=15)')': /artifactory/api/pypi/pypi/simple/requests/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x00000239A35FA500>, 'Connection to repos.proxy.l-3com.com timed out. (connect timeout=15)')': /artifactory/api/pypi/pypi/simple/requests/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x00000239A35FA740>, 'Connection to repos.proxy.l-3com.com timed out. (connect timeout=15)')': /artifactory/api/pypi/pypi/simple/requests/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.HTTPSConnection object at 0x00000239A35FA980>, 'Connection to repos.proxy.l-3com.com timed out. (connect timeout=15)')': /artifactory/api/pypi/pypi/simple/requests/
ERROR: Could not find a version that satisfies the requirement requests (from versions: none)
ERROR: No matching distribution found for requests
With Proxy option:
C:\Users\vdhatchanamoorthy>pip install requests --proxy servername:XXXX
*Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://repos.proxy.l-5com.com/artifactory/api/pypi/pypi/simple
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 '))': /artifactory/api/pypi/pypi/simple/requests/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 '))': /artifactory/api/pypi/pypi/simple/requests/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 '))': /artifactory/api/pypi/pypi/simple/requests/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 '))': /artifactory/api/pypi/pypi/simple/requests/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 400 '))': /artifactory/api/pypi/pypi/simple/requests/
ERROR: Could not find a version that satisfies the requirement requests (from versions: none)
ERROR: No matching distribution found for requests*
Thanks, Venkat
Bartosz Zaczyński RP Team on Dec. 3, 2024
@dakshnavenki It seems that, regardless of whether the --proxy
option is specified, pip
is routing through your company’s local PyPI server, which appears to be hosted on top of Artifactory. The timeout and tunnel errors indicate that the issue might be due to one of the following:
- Incorrect proxy server or port configuration
- Invalid proxy credentials
- The proxy server blocking access to the repository URL
- A misconfigured or non-functional proxy server
It’s difficult for me to pinpoint the exact cause without further details, but these are some possible areas you might investigate.
dakshnavenki on Dec. 4, 2024
Thank you for the response, Bartosz. I would try to get in touch with IT team to resolve this and get back to you if I need more help on this. Thank you so much.
Venkat
Become a Member to join the conversation.
aravind on Sept. 17, 2019
Hi Dan, do you have any other courses where i can learn about pylint and pytest (writing and executing unit tests) and integrating these in a CI pipeline?