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?
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
You can use pip for installing packages. Open command propmpt (cmd) and type this below command
pip install preprocess
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.
Related
In jupyter notebook server, importing numpy or pandas is okay.
pic
However, by using python3 or python in commandline, I am not able to import anything because of a package named SystemRandom. pic2
Can anyone give me some suggestions?
Much Appreciated!
You have a file random.py on your Users folder ("/Users/chenyao/random.py") this file is interfering with the numpy import. You have two options:
rename the file
change directory
It tries to import SystemRandom from /Users/chenyao/random.py. Remove or rename that file and it should work.
I've created multiple python modules as .py files in a Python IDE called Pyzo in the following path:
'C:\Users\Michael\Anaconda3\Lib\site-packages' which I can then import like regular Python packages such as pandas and numpy into my Jupyter notebook or into Pyzo.
I'm a bit lost as to how to create a module in Jupyter notebook, containing a class with say a simple function, which I can then save and import into a new Jupyter notebook file.
The examples in this link below I found extremely vague and overly complicated. Any simpler examples would help, thanks!
http://nbviewer.jupyter.org/github/ipython/ipython/blob/master/examples/IPython%20Kernel/Importing%20Notebooks.ipynb
%run ./module_code.ipynb
keep this in import section- replace module_code with your file name and then you can access functions inside that file from the new notebook.
Suppose you want to import the contents of A.ipynb into B.ipynb.
Installation
pip install import-ipynb
How to use
Place both ipynb files in the same directory. Then, in the B.ipynb:
import import_ipynb
import A
Congratulations! You can now run any functions defined in A.ipynb from B.ipynb!
The easiest way to import user created modules on JupyterLab is to Export Notebook as an executable script from File menu as in screenshot. This will download .py file to your pc or mac. Just drag that downloaded file to your jupyterlab to import.
download notebook as .py file
Now you can import and use that module in other notebooks.
import example
I downloaded XlsxWriter zip file as I cannot use pip because of organisational restrictions. I extracted the zip file. Where inside the python directory should I place the XlsxWriter folder now ?
For most of the cases use pip to install Python modules. It is very easy.
Just do:
pip install XlsxWriter
And then in your script you can do the following:
import xlsxwriter
{...your code goes here}
If somehow you are not able to use pip, please follow this
After that close and re-open your IDLE and check.
I got a library called google-translate-python. https://github.com/terryyin/google-translate-python
Basically, I copied/pasted the translate.py file to my python27/lib directory. I imported it like so:
from translate import Translator
And I put in something like this:
theTranslate = Translator(to_lang="sp")
translation = theTranslate.translate("hello")
And I'm using pycharm btw so I haven't gotten any errors, it is saying the methods are there and everything.
However, I get the error: ImportError: cannot import name Translator
Did I import the library wrong? that's all I can think of. Because the methods are there and running.
I figured it out... the library I was trying to import had the same name as my actual python file. So my python file was called translate.py and my library I was trying to import was called translate. I don't know how to differentiate it.. but changing the name of my python file fixed it. wow.. that took about 3 hours to realize.
Does it show in the list of packages installed under Pycharm interpreter? You need to add the package to this list and then it becomes available to you for import. It is available as one of the packages there.
Based on the github page the package can be installed from the source using:
python setup.py install
Another option is to save the translate.py to the local directory or another directory.
If translate.py is not in the local directory you can add the module path using:
sys.path.append('PATH_TO_TRANSLATE.PY')
If you can't use pip the simplest way to get this installed would be to do download the source code (.zip file) and unzip it.
Open a terminal (where you have access to python) and change to the folder (cd <the path to the folder>) you have unzipped, and then run:
python setup.py install
This will make sure the files end up in the right location (which on Windows is actually in C:\Python27\Lib\site-packages).
I am working on a box which I don't have root access. However, there is a folder /share which would be accessed for everyone to read and write.
I want to figure out a way to put python libraries so that everyone could access and use them.
I figured out that I can put the egg file in the /share/pythonLib folder and in the python script.
import sys
sys.path.append("/share/pythonLib/foo.egg")
import foo
and it would work for everyone, however, I am not sure every library has egg version. For example, I am trying to install BeautifulSoup4 , however, there is only tar.gz file and I am not sure if it would be possible to convert to egg and ..etc.
OR! I am wrong right at the BEGINNING, and there are indeed some pythonic magics like below:
magicadd /share/pythonLib/foo.tar.gz
import foo
tar.gz is the source code of the library. You should unpack it, and you will find a setup.py script inside. Run:
python setup.py install --prefix=/share/pythonLib
This will create:
/share/pythonLib/lib/python2.7/site-packages/
In your scripts append that path to sys.path and everything should work fine.