Google Colaboratory - AttributeError: module X has no attribute Y - python

I'm trying to use the Freesound API in a Google Colaboratory notebook (running Python 3) to generate a database of sounds for which to do machine learning with. However I have been unable to use the definitions in a module I imported.
I've looked at other similar questions but they did not seem to address my issue (most were cases of trying to import a module in the standard python library and instead importing a .py file of the same name) and I apologize if this particular issue has been covered somewhere else.
The boilerplate code is as follows:
#clone relevant Git repo
!git clone https://github.com/MoltenMuffins/freesound-python
!ls
#Import packages
import os
import sys
import requests
#Open module file and import module
open('freesound.py','wb')
import freesound
There is some code after that but it is not relevant to the issue. Running this last code block is what gives me the Attribute Error despite FreesoundClient being defined in the freesound.py file cloned from the repo:
freesound_client = freesound.FreesoundClient()
I would greatly appreciate an explanation regarding this issue!
Here's a link to the colabs notebook if you'd like to take a look

I would follow the repo's instructions of using their setup.py to do the installation:
After cloning the git repo, you want to change your working directory to the freesound-python directory and run setup.py
import os
os.chdir('/content/freesound-python')
!python setup.py install
# now import the module
import freesound

Related

How to import own modules from repo on Databricks?

I have connected a Github repository to my Databricks workspace, and am trying to import a module that's in this repo into a notebook also within the repo. The structure is as such:
Repo_Name
Checks.py
Test.ipynb
The path to this repo is in my sys.path(), yet I still get ModuleNotFoundError: No module named 'Checks'. When I try to do import Checks. This link explains that you should be able to import any modules that are in the PATH. Does anyone know why it might still not be working?
I have tried doing the same and got a similar error even after following the procedure as given in the link provided in the question.
I have the following python files in my GIT repo (3 files with .py extension).
Now when I add the path /Workspace/Repos/<username>/repro0812 to sys.path and try to import the sample module from this repo, it throws the same error.
This is because, for some reason, this file is not being rendered as a python file. When I open the repo, you can actually see the difference.
There was no problem while I import the other 2 python modules check and sample2. The following is an image for refernce.
Check and make sure that the file is being considered as a .py file after adding your repo.

Question about Colab importing your own modules

I am relatively new to Colab & python, so I don't really know how to use it. I have git-cloned a GitHub repository onto my Colab notebook. There are some modules inside the GitHub folder that I have cloned that I need to import them into the notebook. For example, there is a utils and models module that I want to import. I ran the code:
from models import *
from utils import *
However, the Colab editor wrought the error ModuleNotFoundError: No module named 'models' and ModuleNotFoundError: No module named 'utils'. This led me to realize that I haven't imported the modules from the Git Hub clone in Colab Files into Colab. I couldn't find an answer on StackOverflow, w3schools, or the Colab official sites so I
wrought my question here. If any of you have a link to a guide or a solution, please help me. Thank you!
Yes, some of the Jupyter Notebook tricks worked. I used %cd pytorch_objectdetecttrack to get to the modules.
Not an expert in Colab, but for Python import only works for Python modules. So if there's a specific script you're trying to import, you will have to call it like import folder.someModule. The folder you're trying to access should go first, then the module name.
It's the same as saying ./folder/someModule.py.
tl:dr; You have to specify the path to the module if it's not in the current working directory.

Trouble setting up libraries imported from github

I'm trying to clone a repo to my machine to test changes for a pull request.
The repo in question is a clone of pytorch, and I want to add something to one of the files to fix an issue. I figured how to clone the repo, but I can't figure out how to import the pytorch libraries when I write a test file that contains something like:
import torch
x = torch.rand(5, 3)
print(x)
Where am I supposed to create a test.py file? How do I add pytorch (specifically my cloned version of pytorch) to the list of dependencies for Python to run with? I tried just creating a test.py file at the same level as the cloned repo, but i get the error message
"no module named torch.version". I am using VS code.
I'm new to using git and not extremely familiar with the structure of libraries like this. I tried looking through github, stack overflow and the pytorch docs but was unable to find an explanation.
Make sure your current selected interpreter doesn't contain pytorch, then before import torch, add the following code:
import sys
sys.path.append("\the folder that contains pytorch\")
Please have a try.

Problems importing custom library in google colab

I am doing some machine learning project and want to run the project on google colab since my own machine is too weak for it and hangs when i try to run the project on it. My project has the structure as shown in picture .
project structure.
I have multiple .py files each importing modules from one another. I converted the project to .zip file in my pc and then used the upload tab on google colab to upload the project. i unzipped the file and tried to run one code from the "examples" folder which is importing some function from the modAL function like this
from modAL.models import ActiveLearner.
this import is failing on google colab with error " no module named modAL " . Can someone please tell me how to get around this issue? The code works just fine on my own laptop.
I found this explanation: https://zerowithdot.com/colab-workspace/ - very useful.
After creating a space in your google drive
from os.path import join
from google.colab import drive
ROOT = "/content/drive"
drive.mount(ROOT)
fetch the git repo
GIT_USERNAME = "xxx"
GIT_TOKEN = "xxx"
GIT_REPOSITORY = "Repo"
!mkdir "{PROJECT_PATH}"
!git clone https://{GIT_TOKEN}#github.com/{GIT_USERNAME}/{GIT_REPOSITORY}.git "
{PROJECT_PATH}"
Finally use importlib to get access to definitions
from importlib.machinery import SourceFileLoader
somemodule = SourceFileLoader('somelib', join(PROJECT_PATH,
'utils/somelib.py')).load_module()
If the project is public (probably possible to make it work otherwise too) you can create a package [1] and install it with pip:
!pip install git+https://github.com/myuser/myproject
[1] https://packaging.python.org/tutorials/packaging-projects/

How can jupyter access a new tensorflow module installed in the right path?

Where should I stick the model folder? I'm confused because python imports modules from somewhere in anaconda (e.g. import numpy), but I can also import data (e.g. file.csv) from the folder in which my jupyter notebook is saved in.
The TF-Slim image models library is not part of the core TF library. So I checked out the tensorflow/models repository as:
cd $HOME/workspace
git clone https://github.com/tensorflow/models/
I'm not sure what $HOME/workspace is. I'm running a ipython/jupyter notebook from users/me/workspace/ so I saved it to:
users/me/workspace/models
In jupyter, I'll write:
import tensorflow as tf
from datasets import dataset_utils
# Main slim library
slim = tf.contrib.slim
But I get an error:
ImportError: No module named datasets
Any tips? I understand that my tensorflow code is stored in '/Users/me/anaconda/lib/python2.7/site-packages/tensorflow/init.pyc' so maybe I should save the new models folder (which contains models/datasets) there?
From the error "ImportError: No module named datasets"
It seems that no package named datasets is present.
You need to install datasets package and then run your script.
Once you install it, then you can find the package present in location "/Users/me/anaconda/lib/python2.7/site-packages/" or at the location "/Users/me/anaconda/lib/python2.7/"
Download the package from https://pypi.python.org/pypi/dataset and install it.This should work
You can find the folder address on your device and append it to system path.
import sys
sys.path.append(r"D:\Python35\models\slim\datasets")
import dataset_utils
You'll need to do the same with 'nets' and 'preprocessing'
sys.path.append(r"D:\Python35\models\slim\nets")
import vgg
sys.path.append(r"D:\Python35\models\slim\preprocessing")
import vgg_preprocessing

Categories