How to avoid pip install global packages accidentally?











up vote
0
down vote

favorite












Sometimes I forget to activate the virtual environment before executing the pip install command, which causes a bunch of useless global pip packages. Some of them are not able to be uninstalled directly using pip (dateutils issue).

I wonder how I can avoid such accidents?

Can I disable pip for non-root users and only permit sudo pip?










share|improve this question


















  • 1




    As far as I know, pip will install to your home directory if you didn't run sudo. So, you should be able to delete what you've accidentally installed. But something you could do that addresses your question is create an alias in .bash_aliases so that pip maps to something (i.e., an echo command that laughs at you :-) ). What I don't know is if you activate a virtual environment, whether the path to its pip is guaranteed to precede the alias. If this doesn't work, you can add a pip executable file and a path leading to it.
    – Ray
    2 days ago






  • 1




    I'm not sure if this is what you're looking for. FWIW, I had sudo access on a (shared) server and I sometimes stupidly do a sudo reboot on it instead of the (unshared) desktop I'm on. (i.e., I type it so quickly that I didn't think carefully...) To "solve" this, I aliased "sudo" to give me a message on the server. It wasn't an elegant solution, but it solved my problem.
    – Ray
    2 days ago















up vote
0
down vote

favorite












Sometimes I forget to activate the virtual environment before executing the pip install command, which causes a bunch of useless global pip packages. Some of them are not able to be uninstalled directly using pip (dateutils issue).

I wonder how I can avoid such accidents?

Can I disable pip for non-root users and only permit sudo pip?










share|improve this question


















  • 1




    As far as I know, pip will install to your home directory if you didn't run sudo. So, you should be able to delete what you've accidentally installed. But something you could do that addresses your question is create an alias in .bash_aliases so that pip maps to something (i.e., an echo command that laughs at you :-) ). What I don't know is if you activate a virtual environment, whether the path to its pip is guaranteed to precede the alias. If this doesn't work, you can add a pip executable file and a path leading to it.
    – Ray
    2 days ago






  • 1




    I'm not sure if this is what you're looking for. FWIW, I had sudo access on a (shared) server and I sometimes stupidly do a sudo reboot on it instead of the (unshared) desktop I'm on. (i.e., I type it so quickly that I didn't think carefully...) To "solve" this, I aliased "sudo" to give me a message on the server. It wasn't an elegant solution, but it solved my problem.
    – Ray
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Sometimes I forget to activate the virtual environment before executing the pip install command, which causes a bunch of useless global pip packages. Some of them are not able to be uninstalled directly using pip (dateutils issue).

I wonder how I can avoid such accidents?

Can I disable pip for non-root users and only permit sudo pip?










share|improve this question













Sometimes I forget to activate the virtual environment before executing the pip install command, which causes a bunch of useless global pip packages. Some of them are not able to be uninstalled directly using pip (dateutils issue).

I wonder how I can avoid such accidents?

Can I disable pip for non-root users and only permit sudo pip?







pip






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









Ssuching Yu

307




307








  • 1




    As far as I know, pip will install to your home directory if you didn't run sudo. So, you should be able to delete what you've accidentally installed. But something you could do that addresses your question is create an alias in .bash_aliases so that pip maps to something (i.e., an echo command that laughs at you :-) ). What I don't know is if you activate a virtual environment, whether the path to its pip is guaranteed to precede the alias. If this doesn't work, you can add a pip executable file and a path leading to it.
    – Ray
    2 days ago






  • 1




    I'm not sure if this is what you're looking for. FWIW, I had sudo access on a (shared) server and I sometimes stupidly do a sudo reboot on it instead of the (unshared) desktop I'm on. (i.e., I type it so quickly that I didn't think carefully...) To "solve" this, I aliased "sudo" to give me a message on the server. It wasn't an elegant solution, but it solved my problem.
    – Ray
    2 days ago














  • 1




    As far as I know, pip will install to your home directory if you didn't run sudo. So, you should be able to delete what you've accidentally installed. But something you could do that addresses your question is create an alias in .bash_aliases so that pip maps to something (i.e., an echo command that laughs at you :-) ). What I don't know is if you activate a virtual environment, whether the path to its pip is guaranteed to precede the alias. If this doesn't work, you can add a pip executable file and a path leading to it.
    – Ray
    2 days ago






  • 1




    I'm not sure if this is what you're looking for. FWIW, I had sudo access on a (shared) server and I sometimes stupidly do a sudo reboot on it instead of the (unshared) desktop I'm on. (i.e., I type it so quickly that I didn't think carefully...) To "solve" this, I aliased "sudo" to give me a message on the server. It wasn't an elegant solution, but it solved my problem.
    – Ray
    2 days ago








1




1




As far as I know, pip will install to your home directory if you didn't run sudo. So, you should be able to delete what you've accidentally installed. But something you could do that addresses your question is create an alias in .bash_aliases so that pip maps to something (i.e., an echo command that laughs at you :-) ). What I don't know is if you activate a virtual environment, whether the path to its pip is guaranteed to precede the alias. If this doesn't work, you can add a pip executable file and a path leading to it.
– Ray
2 days ago




As far as I know, pip will install to your home directory if you didn't run sudo. So, you should be able to delete what you've accidentally installed. But something you could do that addresses your question is create an alias in .bash_aliases so that pip maps to something (i.e., an echo command that laughs at you :-) ). What I don't know is if you activate a virtual environment, whether the path to its pip is guaranteed to precede the alias. If this doesn't work, you can add a pip executable file and a path leading to it.
– Ray
2 days ago




1




1




I'm not sure if this is what you're looking for. FWIW, I had sudo access on a (shared) server and I sometimes stupidly do a sudo reboot on it instead of the (unshared) desktop I'm on. (i.e., I type it so quickly that I didn't think carefully...) To "solve" this, I aliased "sudo" to give me a message on the server. It wasn't an elegant solution, but it solved my problem.
– Ray
2 days ago




I'm not sure if this is what you're looking for. FWIW, I had sudo access on a (shared) server and I sometimes stupidly do a sudo reboot on it instead of the (unshared) desktop I'm on. (i.e., I type it so quickly that I didn't think carefully...) To "solve" this, I aliased "sudo" to give me a message on the server. It wasn't an elegant solution, but it solved my problem.
– Ray
2 days ago










1 Answer
1






active

oldest

votes

















up vote
1
down vote













I am not as good at writing bash scripts but you can replace the pip command with an alias in your bashrc with this script



if [[ $(python -c 'import sys; print (sys.real_prefix)' 2>/dev/null) ]]; then
/path/to/actual/pip/executable install $1;
else
echo "not in a virtual env";
fi


This script solve your problem. If there is anyone who can find an issue in the script or improve it, please do.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1094103%2fhow-to-avoid-pip-install-global-packages-accidentally%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    I am not as good at writing bash scripts but you can replace the pip command with an alias in your bashrc with this script



    if [[ $(python -c 'import sys; print (sys.real_prefix)' 2>/dev/null) ]]; then
    /path/to/actual/pip/executable install $1;
    else
    echo "not in a virtual env";
    fi


    This script solve your problem. If there is anyone who can find an issue in the script or improve it, please do.






    share|improve this answer



























      up vote
      1
      down vote













      I am not as good at writing bash scripts but you can replace the pip command with an alias in your bashrc with this script



      if [[ $(python -c 'import sys; print (sys.real_prefix)' 2>/dev/null) ]]; then
      /path/to/actual/pip/executable install $1;
      else
      echo "not in a virtual env";
      fi


      This script solve your problem. If there is anyone who can find an issue in the script or improve it, please do.






      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        I am not as good at writing bash scripts but you can replace the pip command with an alias in your bashrc with this script



        if [[ $(python -c 'import sys; print (sys.real_prefix)' 2>/dev/null) ]]; then
        /path/to/actual/pip/executable install $1;
        else
        echo "not in a virtual env";
        fi


        This script solve your problem. If there is anyone who can find an issue in the script or improve it, please do.






        share|improve this answer














        I am not as good at writing bash scripts but you can replace the pip command with an alias in your bashrc with this script



        if [[ $(python -c 'import sys; print (sys.real_prefix)' 2>/dev/null) ]]; then
        /path/to/actual/pip/executable install $1;
        else
        echo "not in a virtual env";
        fi


        This script solve your problem. If there is anyone who can find an issue in the script or improve it, please do.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        akabhirav

        1,09021132




        1,09021132






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1094103%2fhow-to-avoid-pip-install-global-packages-accidentally%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Category:香港粉麵

            List *all* the tuples!

            Channel [V]