Is that possible to import from different modules in google colaboratory? - google-colaboratory

I am using google colaboratory. I have a 'utils.ipynb' which contains some useful functions. For example:
load_image(path)
I have another 'models.ipynb' in which I want to use the codes in 'utils.ipynb'. How should I do that? For example, how can I import load_image from 'utils.ipynb'?
This code does not work by the way:
from utils import load_image

You can import from a .py file, not from a .ipynb file.
So,
copy the code in .ipynb into a text file and name it utils.py
upload utils.py to Google Colab
from utils import load_images

Related

I have to import a numpy file (npy) to a scrypt (.py 2.7), don't know how?

I have a numpy file that I can't share, that has to be imported in a python 2.7 (yes ... I need to work with 2.7 for this...) and I dont know how to do this, or how to know if it's recognized or how to see whats inside.
Can some one explain, please how can I see what's inside the numpy file, and how to call it from the script?
Thanks ;)
I've tried importing it, but it doesn't find it
edited: I've tried with this: np.load('preflop.ea.npy') But doesn't seem to work (I've tried to print it and it doesn't print).
enter image description here

Store in a variable all files from a folder react-native

Is there any way to store in a variable all the files from a folder ?
In my case, I want to store all the mp3 files from a folder in a variable
Something like:
import * as files from "./folder"
?
Literally you can not just use import * as files from "./folder" to import mp3 file,
if you want to import an Mp3 file named sound, and its in your folder you need to use:
var sound = require('./folder/sound.mp3');
But it doesn't mean you can store the mp3 data
you need library such as react native fs

How to open the .wadl file for the doc.atlassian.com?

I download a .wadl file from https://docs.atlassian.com/software/jira/docs/api/REST/8.8.1/jira-rest-plugin.wadl
Do you know how to use it? Does it need to be imported by specific application?
You can import your .wadl file with Postman.
Click Import
Select your file or folder, input your link, or paste your raw text. Postman will automatically recognize Postman data.
Confirm the name, format, and what you would like your data to import as, then click Import to bring your data into Postman.
https://i.stack.imgur.com/WMrNE.png
https://i.stack.imgur.com/JYMmE.png

Trying to view decision tree in my notebook

I am trying to scale my decision tree to fit notebook but it appears not to scale properly. I have to keep scrolling for a better view. Can I please have some help on how to fix this. Attach is a pic of how it looks like.
from graphviz import Source
from sklearn import tree
from IPython.display import SVG
graph = Source( tree.export_graphviz(dt_classifier, out_file=None, feature_names=X.columns))
SVG(graph.pipe(format='svg'))
Perhaps it's not relevant any more, since this question has been open for about six months now. However, I just stumbled into it, as apparently 83 other readers, and I just crafted my way around this. The easy way is to use the pydot package (pip install pydot), and then add the default size. I have also been using %matplotlib inline so that it displays nicely within the notebook but without using the svg module. With your example:
%matplotlib inline
from graphviz import Source
from sklearn import tree
import pydot
dot_data = tree.export_graphviz(dt_classifier, out_file=None, feature_names=X.columns))
pdot = pydot.graph_from_dot_data(dot_data)
# Access element [0] because graph_from_dot_data actually returns a list of DOT elements.
pdot[0].set_graph_defaults(size = "\"15,15\"")
graph = Source(pdot[0].to_string())
graph
I also added rotate=True to export_graphviz so that it displays in horizontal style, the root of the tree is directly visible, and is easier to follow. Of course, you can play around with size so as to reach something that is acceptable for you.

python: from modules import abc.py does not work

I have recently switched from python 2.7 to python 3.2
considering following folder structure:
~/my_program
~/my_program/modules
where *my_program* is the root of the application, containing main script called main.py
*my_program/modules* is used to store all additional classes and subscripts
in python2.x I was able to import any file "module" the same way I do import standard modules
from modules import abc.py
however, while I try to launch the same program in python3.2 I get the error message saying:
File "/my_program/modules/__init__.py" line 1
import abc, def
ImportError: No module named abc
Please advise
It's a good example you are using, because "abc" is in fact a module in the standard library. So in fact, if you in Python 3 do import abc that will work just fine, but you will get the standard library module.
In Python 2, if you had a local module called abc.py, that import abc would import the local module instead. That means you couldn't import the module from the standard library.
In Python 3, this has been changed, and you now need to use the fully qualified name, in your case probably from modules import abc.
Where are you storing the module you created? When I create a module, I do the following:
Create a folder in the lib\sitepackages folder in your python folder ex: myModules
Save a blank python file named init.py in this folder. The file does not have to contain any syntax. Later on, you can set module requirements in this file if you wish.
Save your module into this folder ex: myGamesModule.py
In idle (or whichever python connected IDE you use) type:
from myModules import myGamesModule or from myModules import myGamesModule as myGmMod
That should import your module and then you can call the classes etc ex myGmMod.Player()
I am sure that this is correct, as I have just done it and it was ok.
I have found that sometimes, if I create a blank text file in the module folder and rename it to init.py, it has caused me problems before. I usually just open IDLE and save a init.py file from there into my module folder and then use whichever IDE I use (sublime) to create and save the rest of the files.
Also, for some reason, the text box has disallowed the underscores I am using in this text so where you see init.py, it should be (underscoreunderscore*init*underscoreunderscore.py without any asterix