I am using jupyter notebook. I have 3 jupyter source files written in python in a folder in the same directory: parser, preprocess, and temp. I am trying to import parser and import preprocess in the temp file so that I can use the methods written in those files. Example: there is method named extract in parser file. I want to use that from temp file. How can I do that?
Asked
Active
Viewed 528 times
5
-
See [here](https://stackoverflow.com/a/72019554/8508004) and [here](https://stackoverflow.com/a/48712291/8508004) for some examples of importing functions from other python files. The first is specific to doing it in an Jupyter `.ipynb` notebook running with a Python kernel; however, this is broadly applicable to the Python ecosystem. – Wayne May 13 '22 at 20:02
-
1Okay, I just read one of your comments, that the other `preprocess` is in a Python-based notebook. That's slightly different. See [here](https://github.com/deathbeds/importnb#import-notebooks-from-files) about `importnb`. It makes what you want to do easier. It's sort of good I suggest importing from a Python script (`.py` source file first) because I'd suggest reading how to do it from a script first because it is the same concepts yet simpler, and then look at how it is being done from a notebook. – Wayne May 13 '22 at 20:07
-
1I just expanded an answer I had about `importnb` to add an example that might help you, see [here](https://stackoverflow.com/a/58086016/8508004). – Wayne May 13 '22 at 20:37
3 Answers
2
The easiest way is to change the files you need to import as py files. As an example, parser.ipynb can be converted to a python file parser.py, and you can import it from another notebook file. If you want to use a function named extract() from parser.py, just import
from parser import extract

batman
- 60
- 6
1
You can use pip for installing packages. Open command propmpt (cmd) and type this below command
pip install preprocess

Anubhav
- 273
- 2
- 8
-
It is not a package. preprocess is also python code I have written in notebook. – mhoq May 13 '22 at 18:10
-
-
-
0
You can run pip install preprocess
from the terminal (or CMD, if you are using Windows). Alternatively, you can run ! pip install preprocess
from Jupyter Notebook itself, which will do the same thing. You may need the second one if you are working on Google Colab.

Jafar Isbarov
- 1,363
- 1
- 8
- 26
-
-
-
@Jafar Isbarov. I know it isn't pertinent now because a published package isn't involved; however, I wanted to clue you in to avoiding suggesting using an exclamation point with `pip`. Basically, it can cause problems and is outdated advice. See my comment [here](https://stackoverflow.com/a/72197390/8508004). Should be `%pip install
`. – Wayne May 13 '22 at 20:20 -