import function from another file in colab not working - import

I am trying to import a function from another file different directory in Colab, but it's not working.
I have tried:
1)
!pip install kora -q
from kora import drive
drive.link_nbs()
sys.path(0,"path to file")
import name_file
import ipynb

Related

VSCode Jupyter project deletes lines of python code when saving the new .py file

(1) Create a new .py file
(2) Add code
(3) Ctrl + s
It deletes lines of python code in the file. Why?
import sys, numpy, pandas as pd, matplotlib, seaborn
import matplotlib.pyplot as pyplot
seaborn.set(style="darkgrid")
When ctrl+s, it deletes lines and only the following remain:
import seaborn
seaborn.set(style="darkgrid")

Import Shutil module arttribute

I tried to use shutil module and copytree attribute. But it is not working and the commands box is empty. I import copytree and still not working.

Importing keras-rl package into conda environment

I've installed keras-rl package on my computer, using their instructions:
git clone https://github.com/matthiasplappert/keras-rl.git
cd keras-rl
python setup.py install
So my conda environment sees this package, however when I am trying to import it in Spyder as a part of my code, i.e. import keras-rl, I get the following error:
SyntaxError: invalid syntax
with a pointer to the dash of keras-rl.
Question: How could I import the keras-rl (or any other package with the dash in the name) in Spyder?
We can install keras-rl by simply executing
pip install keras-rl
There are various functionalities from keras-rl that we can make use for running RL based algorithms in a specified environment
few examples below
from rl.agents.dqn import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
This is how we can use the package.
If you look at the examples present in the github repository, you will see that the various functionalities are imported from rl. Like this:
(root) ~/condaexpts/keras-rl/examples $ grep -h import * | grep rl
from rl.agents import ContinuousDQNAgent
from rl.memory import SequentialMemory
from rl.random import OrnsteinUhlenbeckProcess
from rl.core import Processor
from rl.agents.cem import CEMAgent
from rl.memory import EpisodeParameterMemory
from rl.agents import DDPGAgent
from rl.memory import SequentialMemory
from rl.random import OrnsteinUhlenbeckProcess
from rl.agents.dqn import DQNAgent
from rl.policy import LinearAnnealedPolicy, BoltzmannQPolicy, EpsGreedyQPolicy
from rl.memory import SequentialMemory
from rl.core import Processor
from rl.callbacks import FileLogger, ModelIntervalCheckpoint
from rl.agents.dqn import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
I had the same problem. After lots of examination I found the correct way.
You can import RL by writing this:
"import rl"
and then write your code like:
rl.core.Agent()

Import a python file into another python file on pythonanywhere

I am trying to built a web app on pythonanywhere.
I have a file abc.py and another file xyz.py . In the file xyz.py I want to use a function of abc.py so how do I import abc.py into xyz.py given that they are in the same directory. What will be the import statement? I was trying:
import "absolute/path/to/the/file.py" which didnt work.
Also did
from . import abc.py in xyz.py which also didnt work
PythonAnywhere dev here: if they're in the same directory, then you need to use
from abc import functionname
This part of the official Python tutorial explains how import works.

How to import all imports of another .py file

Info
python version: 3
development environment: eclipse luna
Goal
I'm currently developing an addon system for a program. My Idea was to create a file where I import all addons. This file is generated during the addon instalation process (when you click the button: install addon). It looks like that:
import testaddon1
import testaddon2
import bigaddon.startup as bigaddon
When I start my programm I want to import all files/modules to read some properties and automaticly implement the addons in my program.
Question
How can I start import statements that are written in a different file.
file1.py
def test():
print('test')
file2.py
import file1.py.test as test
file3.py
#run the import commands from file2.py
test()
console output after running file3.py:
test
What I want
an answer on how to achieve the previous result
a different idea on how to create an addon system
Yes you can do it.
file.py
def hello():
print('hello')
file2.py
import file
file.hello()
file3.py
from file2 import *
file.hello()
executing file 3. greg#ja python file3.py