Error while importing a custom module in colab - module

I'm trying to import a custom module to my script but colab won't find the module from my drive.
I'll add a screenshot so you can see the directory:
Screenshot of directory
this is the code with which I am trying to import the module:
import numpy as np
import matplotlib.pyplot as plt
!cp "/content/gdrive/My Drive/Colab Notebooks/Heston/black_scholes.py"
import black_sholes
When I try to run this I get the following Error message:
ModuleNotFoundError: No module named 'black_sholes'
Thanks in advance for your help!

You can add your gdrive to PYTHONPATH:
import sys
sys.path.insert(0, "/content/gdrive/My Drive/Colab Notebooks/Heston")
import black_scholes
If you don't want to modify the sys.path you can just run your module, but then you will loose black_scholes namespace:
%run "/content/gdrive/My Drive/Colab Notebooks/Heston/black_scholes.py"

You forgot the dot at the end of
!cp "/content/gdrive/My Drive/Colab Notebooks/Heston/black_scholes.py"
It should be
!cp "/content/gdrive/My Drive/Colab Notebooks/Heston/black_scholes.py" .

Related

Takes very long to import Huggingface packages stored on GDrive in Colab

import os, sys
from google.colab import drive
drive.mount('/content/gdrive')
path = '/content/gdrive/MyDrive/JIW/Libraries'
sys.path.append(path)
# !pip install --target=$path transformers datasets wandb # <- This line can be used to load pkg
from transformers import AutoTokenizer, default_data_collator, AutoModelForQuestionAnswering, TrainingArguments, Trainer
from datasets import Dataset, load_dataset
import wandb
The Transformers/Datasets libraries are saved at the path specified. But the two "from transformers import" and "from datasets import" statements take 5ish minutes to execute on Colab. Is this normal or am I messing up somehow?

module 'tensorflow' has no attribute 'tanh'

I am trying to repeat what is shown in this tutorial: https://www.kaggle.com/alexisbcook/deep-reinforcement-learning
When I run this code:
# Check version of tensorflow
import tensorflow as tf
tf.__version__
I am getting this error:
module 'tensorflow' has no attribute 'version'
When I run this piece of code I am not getting any error:
from kaggle_environments import make, evaluate
from gym import spaces
class ConnectFourGym:
...
Then running this code without error:
# Create ConnectFour environment
env = ConnectFourGym(agent2="random")
But when I try to run the following code
import os
from stable_baselines.bench import Monitor
from stable_baselines.common.vec_env import DummyVecEnv
# Create directory for logging training information
log_dir = "ppo/"
os.makedirs(log_dir, exist_ok=True)
# Logging progress
monitor_env = Monitor(env, log_dir, allow_early_resets=True)
# Create a vectorized environment
vec_env = DummyVecEnv([lambda: monitor_env])
I am getting the following error:
module 'tensorflow' has no attribute 'tanh'
The error is pointing these lines causing the problem:
----> 2 from stable_baselines.bench import Monitor
...
----> 1 from stable_baselines.a2c import A2C
...
----> 1 from stable_baselines.a2c.a2c import A2C
...
----> 9 from stable_baselines.common import explained_variance, tf_util, ActorCriticRLModel, SetVerbosity, TensorboardWriter
...
----> 7 from stable_baselines.common.base_class import BaseRLModel, ActorCriticRLModel, OffPolicyRLModel, SetVerbosity,
...
---> 16 from stable_baselines.common.policies import get_policy_from_name, ActorCriticPolicy ...
--> 375 class LstmPolicy(RecurrentActorCriticPolicy):
Why this is happening? How can I fix this problem?
I fixed the problem by specifying TensorFlow version:
# Check version of tensorflow
!pip install -q 'tensorflow==1.15.0'
import tensorflow as tf
tf.__version__
This code snippet installs TensorFlow version 1.15.0 and prints
'1.15.0'

Colab ModuleNotFoundError

I have been following
https://colab.research.google.com/drive/12qC2abKAIAlUM_jNAokGlooKY-idbSxi
pretty exactly but I still cannot access any of my code from other files
My attempt:
!ls /content/gdrive/My\ Drive/Colab\ Notebooks/*.ipynb
!cat '/content/gdrive/My Drive/Colab Notebooks/module_i_want.ipynb'
import sys
sys.path.append('/content/gdrive/My Drive')
import module_i_want as mod
mod.loadData()
I am getting:
ModuleNotFoundError: No module named 'module_i_want'
I have tried:
import os
print(os.path.dirname(os.path.realpath('module_i_want.ipynb')))
print(os.path.dirname(os.path.realpath('module_im_in.ipynb')))
and they both return
/content
I can see both files to the left in my mounted drive (in the same folder) but I can't seem to find any way to import anything
Resolved:
For .ipynb files in Google Colab you need to specifically install
!pip install import_ipynb
and then:
import import_ipynb
in order to deal with this type of file

How to install and use IB API within Colab?

I am creating a notebook on Google Colab to run trading algorithms which execute on Interactive Brokers (IB). I want to use the IB API for this.
My current code downloads and installs the API:
# Install IB and related libraries
!wget -cq http://interactivebrokers.github.io/downloads/twsapi_macunix.975.01.zip
!unzip -qq -o twsapi_macunix.975.01.zip
!cd IBJts/source/pythonclient && python setup.py build install
!pip install ib_insync
!pip install ibapi
# Import generic libraries
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import datetime # For datetime objects
import os.path # To manage paths
import sys # To find out the script name (in argv[0])
import pandas as pd
import numpy as np
import matplotlib as plt
import time
from datetime import datetime
import argparse
# Import IB and IB_InSync, and start an IB connection
from ib_insync import *
from ibapi import *
The last line returns an error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-3cdef93fab15> in <module>()
48 from ib_insync import *
---> 49 from ibapi import *
ModuleNotFoundError: No module named 'ibapi'
As the installation seems to run properly, I do not understand why I cannot import the API for use in the subsequent code.
Thanks already for your help!
Found a workaround by installing the library directly from the .egg file:
'''
ib_path='/usr/local/lib/python3.6/dist-packages/ibapi-9.75.1-py3.6.egg'
sys.path.append(ib_path)
from ibapi import *
'''

tensorflow object detection API ImportError when generating PASCAL TFRecord files

I'm trying to use the Tensorflow Object Detection API and I've successfully tested the installation,but we I try to generate the PASCAL VOC TFRecord files with the given command
python object_detection/create_pascal_tf_record.py \
--label_map_path=object_detection/data/pascal_label_map.pbtxt \
--data_dir=VOCdevkit --year=VOC2012 --set=train \
--output_path=pascal_train.record
I encountered the following error:
Traceback (most recent call last):
File "object_detection/create_pascal_tf_record.py", line 36, in <module>
from object_detection.utils import dataset_util
ImportError: No module named object_detection.utils
my PYTHONPATH is:
:/usr/local/lib/python2.7/dist-packages/tensorflow/models:/usr/local/lib/python2.7/dist-packages/tensorflow/models/slim
and I'm running the above command in the /models directory,anyone who knows how to fix this problem?
I had the same problem and I solved it by adding :
import os
import sys
sys.path.append(os.path.abspath("./object_detection"))
and
from object_detection.utils import dataset_util
becomes
from utils import dataset_util
It's better for you to add 'object_detection' to the env path, not in the python script.
You can do this in the shell, at the 'object_detection' directory.
export PYTHONPATH=$PYTHONPATH:`pwd`
Or add the path to .bashrc/.zshrc (depend on your shell), so don‘t need to export every time.
echo "export PYTHONPATH=$PYTHONPATH:`pwd` >> ~/.bashrc