'Google Colab: pip install and imports from a custom script
In MyDrive/colab a I have these files:
00-imports.py with content...
!pip install boto3
classes.py with content...
class Test
def __init__(self):
print('test objecjt created')
def test1(self):
print('test::test1')
Now, my notebook looks like this...
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
import sys
sys.path.insert(0, '/content/drive/MyDrive/colab')
from classes import Test
t = Test()
print(t.test1)
import boto3
AWS_ACCESS_KEY_ID = '****'
AWS_SECRET_ACCESS_KEY = '****'
s3 = boto3.client("s3", aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
If I run it....
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-a227f9818978> in <module>()
3 AWS_SECRET_ACCESS_KEY = 'YgevaTvOHLs/fqfkoZ/MpX+kOMQa14sqhyCfcTTz'
4
----> 5 s3 = boto3.client("s3", aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
NameError: name 'boto3' is not defined
It's any way to call pip from my drive scripts? It's any way to do the imports in those scripts?
The idea is create some libs in order to clean colab notebooks. Revome instalations, imports and clients from main notebook page and hidde all this stuff in my drive scripts.
Solution 1:[1]
I did it this way:
import os
requisite='boto3'
os.system(f"pip install {requisite}")
Solution 2:[2]
You can't install boto3 like that in a .py file If you strictly want to use a python file, you can install it like that:
import subprocess
subprocess.call(['pip', 'install', "boto3"])
or you could make an install.sh script containing your installs
and you can execute that script using !sh install.sh or !bash install.sh
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | m0r |
| Solution 2 | m0r |
