简体   繁体   中英

How to change the path in google colab?

I'm currently working on getting better acquainted with Google Colab and using it as a coding environment with python. I'm trying to setup an ipynb script for testing. As part of this, I'm trying to install pipenv. I'm following the directions from ( https://docs.python-guide.org/dev/virtualenvs/#lower-level-virtualenv )

Part of the directions state to change the path from ~/.local to ~/.local/bin. I ran command: "!python -m site --user.base" to determine the path, and it returned "/root/.local" I'm not very familiar with Colab so I don't know if a bin file already exist or if I need to make one or something. I'm also unsure of whats the safest and what best practices should be followed with respect to Colab when setting the path. Typically I modify the path on my windows machine via the GUI, and I've never had to change it using the command line before. Thanks in advance to anyone who knows how to do this.

Okay, here is a copy of the error message I was getting from Google Collaboratory while trying to install pipenv:

    WARNING: The script virtualenv is installed in '/root/.local/bin' which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    WARNING: The script virtualenv-clone is installed in '/root/.local/bin' which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
    WARNING: The scripts pipenv and pipenv-resolver are installed in '/root/.local/bin' which is not on PATH.
    Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

Since I wasn't really sure what these warnings meant, I ran the pipenv install command anyway:

    !pipenv install requests

This resulted in an error message basically saying the the command pipenv could not be found.

I don't have any experience setting path variables in Colab or from the command line for that matter, so I reviewed the following stackoverflow question from another user [link]: How can I insert path (environmental variable) for geckodriver in goggle colab? to try and get an idea of how to go about it. After reviewing it, I ran the following command:

    !cp /root/.local/bin /usr/local

This resulted in an auto comment:

    cp: -r not specified; omitting directory '/root/.local/bin'

As I said before, I don't have any experience with setting system variables from command line, and everything I've every read about it comes with a warning saying something like: failure to do it properly can permanently damage your machine. So I was hesitant to just tack on the "-r" flag.
After doing a web search I found a good explanation of the -r (recursive copy) flag from this site [link]: https://superuser.com/questions/839719/why-is-r-recursive-necessary-when-copying-a-directory-in-linux/839749 and I ran the following command:

    !cp -r /root/.local/bin /usr/local

This ran without incident and I called:

    !pipenv install requests

Success! It ran without incident. Since I want to save my results from the script I'm writing, I had previously mounted a virtual drive using:

    from google.colab import drive
    from google.colab import files

    drive.mount('/content/drive')

I also created a new directory in my google drive to hold this script and any future data files etc... I was not in the directory when I ran the above code and I don't know if that will be a problem. I'm not really familiar with Python and its virtual environments. If anyone does know if this will be an issue, please feel free to message me. Thank you to everyone who read this question and attempted to answer, or even contemplated what the answer could be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM