Signup/Sign In
LAST UPDATED: NOVEMBER 16, 2021

Disable Pip outside of Python Virtual Environment (Restricting Pip command)

    Working with Python Virtual Environment let us have a dedicated development environment which keeps its dependencies enclosed in it without disturbing the global system environment. When I say "Global System Environment", I mean your Laptop/computer.

    For Mac users, this is especially very important because Mac comes pre-installed with Python 2.7, hence there are chances that you might end up running the pip command for global scope and thereby update some default python packages used by the OS, which is not desired.

    That is the reason we use virtual environments for development. Also to have separate virtual environments for different projects to avoid errors due to conflicting libraries or packages.

    I have a MacBook, with Python 2.7 pre-installed and Virtual Environment for Python with Python 3.6 installed. Recently I started studying about Tkinter to develop GUI in Python and to practice I decided to download Tkinter using pip, but I forgot to start the virtual environment, and just used the pip command to install Tkinter, which installed the Tkinter package in the global scope.

    This is a very basic example of the problem that we might end up facing if we mistakenly use pip in global site-packages.


    Solution 1: Don't have Pip installed globally

    Doing so, pip will only work in an active virtual environment. To update global site-packages you can use other commands like easy_install. But easy_install is not as powerful as pip, for example, we cannot uninstall any package using easy_install, which is a big turn off. So let's jump to the second solution.


    Solution 2: Restricting Pip to Virtual Environment

    Pip comes with a hidden, less known property require-vitualenv, which can be set to TRUE, to restrict pip to only run when there is an active virtual environment. You can read more about this property here in this question on GitHub and or here in the code for pip itself.

    So all you have to do is, open the pip configuration file and add the following to it,

    require-virtualenv = True

    and then deactivate your virtual environment, if activated, and then run any pip command to see what happens, for example,

    pip3 install xyz-package
    
    Output: Could not find an activated virtualenv (required)

    Doing so solves our problem, but what if you want to update some of your global site-packages, now that you have disabled Pip, it won't work, and everytime you will have to first update the pip configuration file and then run pip command to update global site-packages.

    Well, there is a simple solution to this problem, once and for all. We can turn off this restriction by defining a simple function in ~/.bashrc file.

    gpip() {
    
        PIP_REQUIRE_VIRTUALENV = "" pip3 "$@"
    
    }

    After making the above changes, run the following command to enable the changes made,

    source ~/.bash_profile

    Now whenever you want to update your global packages, run the pip command like this, (below command is to update setuptools)

    gpip install --upgrade setuptools

    If you do not want to define such a function, you can also run the pip command directly like this,

    PIP_REQUIRE_VIRTUALENV="" pip3 install --upgrade setuptools

    but this is a little cumbersome to type every time.

    Hope this helps you in setting up your development environment as per your need. Keep Coding!

    You may also like:

    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS