Is it possible to call function from another Python file on Jupyter? - python

When creating Python script on Jupyter, is it possible to call a function from another Python file?
When I try to call, it works on Terminal but does not work on Jupyter.

For example, suppose I have a Person.py file and it is in the working directory of my current jupyter notebook. Furthermore, suppose it is simple and here it is
def get_rss(y, x):
x = sm.add_constant(x)
results=sm.OLS(y, x).fit()
rss= (results.resid**2).sum()
N=results.nobs
K=results.df_model
return rss, N, K, results
def myfunc(d):
print("Hello my name is " + d)
When I try to call, it works on Terminal but does not work on Jupyter.
When I try to call it as import Person, it works on Jupyter cell but I can not access the functions inside of it.
For example, if you tried writing this in Jupyter Notebook you might get this message after typing this
Person.myfunc('ds')
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-60-63fdc0f35859> in <module>
----> 1 Person.myfunc('ds')
AttributeError: module 'Person' has no attribute 'myfunc'
My Advice
Restart your kernel and this should eliminate this problem. I hope this clarifies your question as well could be considered as an answer.

Just restart the kernel and it should work fine :)

Firstly, if you're already working with other files, I recommend that you stop using jupyter and even terminal aswell.
You can use Pycharm instead.
If you were just learning, you could use jupyter but if you're already at that stage I recommend you to stick to Pycharm.
Pycharm is a nice IDE, where you can run the files directly on it, and has a lot more cool features.
Also, to use it in jupyter, just import the files you want:
import #whatever file you want

Related

Can I test a function defined in a file manually in the terminal in vscode?

I have previously only written python code using IDLE but since I am starting to do some more "heavier" programming I figured I should start using Visual Studio Code. I am however having issues doing things that I would like to do while coding to check that my functions are working as intended. The major thing I want to be able to do is if I have saved
def summa(x, y)
return x+y
in a file sum.py, then I would want to test run summa(3, 4) in the terminal.
In IDLE I am used to just running the file containing the function and then use it but I cannot figure out how to do that in Visual Studio Code. However, I realize that it is possible to import the file into a REPL terminal but I would hope that there is some easier way of doing it.
See the answers to this question :
execute python script with function from command line, Linux
You could also use the main function :
if __name__ == '__main__':
summa(sys.argv)
So you just launch the script and it run the function
You can start Python interactively and import your file as a module. Then your function will be available, so you can call it the way you want:
What I did was:
Open new terminal
Type: $ python to start an interactive session
Import module import test or the function from the module: import summa from test
Now I can call summa() and play with it in this manual manner

how to call Python program

I am running a program darknet.py from within my ubuntu terminal by typing python3 darknet.py.
It works perfectly and now I am trying to include it in my own program. But when I call it I get errors. I am not sure what is causing this.
I have already included #!usr/bin/env python3 but no luck.
I am trying to call it with os.systems('/home/bob/DarknetAB/darknet/darknet.py')
The errors I get are as follows
from:cant read /var/mail/cytpes
import -im6.q16: not authorized math#error/constitute.c/writeImage1037
I don't know get why it would work from terminal, but not when I call it from another file?
Did you try to do the following?
import darknet
This is a guess, but it sounds like it could be trying to read your python script like a bash one instead. What about
os.system('python3 /home/bob/DarknetAB/darknet/darknet.py')

Asking for overwrite while trying to run script in ipython

I am fairly new to programming in python. I installed anaconda and am running iPython (the Jupyter qtconsole) v.4.3.0 and python v.3.6 on a Mac. Currently, I am trying to import a module with functions located in my home directory.
I have looked at stackoverflow and python documentation and found that it could be done with:
%run "Users/myUser/python_functions.py"
or
import python_functions
However, when I try both of these approaches, I get prompted to overwrite the file that I am running or importing:
File `python_functions.py` exists. Overwrite (y/[N])?
This is changing the previous file and not getting the functions I want to be imported.
What may explain this, and what can I do to import my module?
this is wrong but leaving it up for shame
import on ubuntu (and I'm guessing many other unix-like OSs including Mac) is a utility that saves any visible window on an X server and outputs it as an image file. You can capture a single window, the entire screen, or any rectangular portion of the screen.
My guess if you are running the import command in your console, and it's about to take a screenshot and save it over an existing file - python_functions
Before you the use the python import command, start a python interpreter:
$ python
>>>import yourfile
edit: on re-reading your question, I'm not so sure about my guess anymore, but leaving it up until you tell me I'm wrong :)
Running Jupyter qtconsole as an interpreter is likely causing the problem in this scenario. Instead using a IDE or command line interpreter will resolve it .
Since anaconda was installed, trying it with the IDE Spyder executes the code just fine without the overwrite prompt. It works on others (e.g PyCharm, Rodeo, etc.) as well.

Python code get error in eclipse while it runs fine in terminal

I tried to run sklearn example, when I try to run it via terminal everything works fine but when I copy and paste this code in eclipse and try to run it I get this error:
Traceback (most recent call last): File "/Users/ABC/Documents/Eclipse/workspace/project/src/sklearn/plot_ROC.py", line 9, in <module>
from sklearn import svm, datasets ImportError: cannot import name svm
I check that both eclipse and system using the same version of python (at least I think so)
In terminal when I typed "which python" the result is "//anaconda/bin/python"
And in eclipse I set the same for Python interpreter as you can see in below picture.
So can anyone guide me what is the problem?
you may compare Python path under Eclipse and terminal by
import sys
print ( sys.path )
the order of entries does matter. My guess is that the fact that your Python file is in a folder called sklearn ( i.e .../project/src/sklearn) is messing the import statement.
requested edit: as it turned out, the problem was that the Python script was in a folder called sklearn and Python would look into that folder to import svm.

Python ImportError with Sage

Okay I am newer to python and have been researching this problem but I can't find anything like it so I am not sure what is going on.
I am creating a program that involves sage and it has a message cue. We have this set up on a development machine, so I know it works but I was wanting to set it up on my own computer so I could get a better understanding of how it all works and make it easier to develop for myself.
To start up sage, we run a script that calls sages main binary file and passes it an executable .py file. (./sage/sage ./sage_server.py) This creates an error in the sage_server.py file:
Traceback (most recent call last):
File "./sage_server.py", line 23, in <module>
from carrot.messaging import Publisher
ImportError: No module named carrot.messaging
But whenever I run that file just in the terminal (./sage_server) the import works fine and isn't until line 27 that there is an error when it tries to import something from sage.
Does anyone know what would cause the error when it is being called by something else? I am very lost as to what would be causing this.
Sage has its own python, separate from the system libraries. This "carrot" module, whatever it is, must be installed in whatever python ./sage_server.py uses, but not in Sage.
You should be able to use either
[your-sage] -sh
to start up a Sage shell and use easy_install, or you could get whatever carroty package you're using, find its setup.py file, and then run
[your-sage] -python setup.py install
where obviously your-sage is the path to your sage.
Things get a little trickier if the install process isn't setup.py-based.

Categories

Resources