How to install a Python package from within IPython? - python

I wonder if it's possible to install python packages without leaving the IPython shell.

See the accepted answer from #Chronial which is the best way to do this in modern ipython or jupyter (as of 2018) is to use the %pip magic:
%pip install my_package
The answer below from 2011 is now outdated: See the accepted answer for an easier way to do this in modern jupyter.
You can use the ! prefix like this:
!pip install packagename
The ! prefix is a short-hand for the %sc command to run a shell command.
You can also use the !! prefix which is a short-hand for the %sx command to execute a shell command and capture its output (saved into the _ variable by default).

This answer is outdated: See the accepted answer for an easier way to this in modern jupyter.
aculich's answer will not work in all circumstances, for example:
If you installed ipython/jupyter in a venv and run it directly via the venv's python binary
If you have multiple python versions, like EntryLevelR.
The correct command is:
import sys
!{sys.executable} -m pip install requests

The best way to do this in modern ipython or jupyter is to use the %pip magic:
%pip install my_package

import pip
pip.main(['install', 'package_name'])
The above shell-based answers don't work unless pip is in your $PATH (e.g. on Windows).

I like hurfdurf's answer, but on its own iPython may not recognize the new module (especially if it adds to the library path). Here's an augmented example with iPython 3:
import pip
pip.main(['install','pygame'])
# import pygame at this point can report ImportError: No module named 'pygame'
import site
site.main()
# now with refreshed module path...
import pygame

In case you are using Conda Package Manager, the following syntax might fit your needs
$ conda install -c conda-forge <targetPackageName>
https://pypi.org/project/earthpy/

!pip install packagename or similar codes will not be the true answer if we have various python versions, where the desired python version is not the system default one. Such codes will install packages on the system default python version only. From my opinion, the following code would be helpful:
import pip
if int(pip.__version__.split('.')[0])>9:
from pip._internal import main
else:
from pip import main
main(['install', 'package_name']) # the strings in the square brackets could be specified as needed
This code is written based on pip version, which can be run from within the console and will be applied on any python versions which path is set by the console; This python version specs could be seen by import sys; sys.version, sys.path. I'm not sure this is the best approach, but the aforementioned code worked for my purposes and !pip install packagename or %pip install packagename did not (where python: 2.7.9, pip: 19.2.3, IPython QtConsole: 3.1.0).

Related

No module named 'ffnet' in Python

I try a python code for signature recognition, and there is an import ffnet module (from ffnet import mlgraph, ffnet), but I got an error when I run it.
The error is:
ModuleNotFoundError: No module named 'ffnet'
I have install the module, but still got that error
Help me to fix this :)
You need to make sure that it is correctly installed. The error message means directly "You haven't installed it properly".
Depending on what Python version you're using, you should have a package manager called pip that takes charge of installing and uninstalling modules. Try:
pip2 install ffnet if you have Python 2.
pip3 install ffnet if you have Python 3.
Alternatively, you may have installed Python using Anaconda. In this case, use conda install ffnet. In all cases, run the proposed commands in a terminal.
However, it would be quite useful to have more details about your problem (what OS do you have, how and where did you install Python, what version do you have).
There is great chance that the pip (i suppose you use pip for installation, the idea is identical) you use to install ffnet is not correspond to the python you are using. Maybe a virtualenv is running, or you using python 2 but ffnet is installed with pip3
My suggestion:
- Run which pip. Run which python. Compare the results if anything seem wrong (python2 pip3 for example). Try to run python2 and pip2 instead of python and pip
- If the above suggestion doesn't work, you should try to recheck your PATH: Find the pip correspond to your current python (usually within the same dir) and export PATH=/path/to/THAT/pip/:$PATH
- If the problem still persist, I suppose your pip file's first line (for specifying its corresponding python path) has been modified without your awareness. You will have to manually edit it to something like #!/usr/bin/python3
Hope this help!

ImportError: No module named IPython

When i try to use from IPython.display import clear_output, display_html, then i show the error:
(ImportError: No module named IPython)
I am using Python 2.7.13, and im trying to make the game of life from John Conway. I am following this link: http://nbviewer.jupyter.org/url/norvig.com/ipython/Life.ipynb
I have read another questions and answers about it, but any answer works for me. First, this error is showed in command line, and when i try to run this code in a file. Second, this error is direct to IPython, not submodule or something similiar.
Ok, finally i achieved my goal.
I wrote ipython --version but i found, it was not installed.
I tried to install it, with pip. I went to C:\Python27\Scripts, here is pip, you can try in this directory, or add to environment variables.
I tried to install ipython, but i found a error
error: Unable to find vcvarsall.bat
so i installed visual studio c++ 9.0, the version to python 2.7.
pip install ipython
If you scrolled this far you may want to try:
import IPython
as opposed to import Ipython. Notice that 2 letters are capitalized
For Anaconda try,
conda install -c anaconda ipython
Use this code to install the IPython library:
!pip install ipython
import IPython
Well, this works on Google Colab.
This is most likely because ipython is not installed.
You can install it with pip.
pip install ipython
If you are using Anaconda (the full version) ipython comes preinstalled. If you are using Miniconda or if the ipython is deleted for some reason you can re-install with
conda install -c anaconda ipython
If you are using it with jupyter. You might want to register the ipython with a user.
python -m ipykernel install [--user] [--name <machine-readable-name>] [--display-name <"User Friendly Name">]
Reference :
Official Documentation
I have a similar issue, but it appears when I was running the script under sudo. Fast and easiest way was to install IPython under sudo.
sudo pip3 install IPython
I am running a script that uses IPython module, in my terminal. If you are also trying to do something similar, this answer might help you.
!pip3 install IPython
Things to keep in mind:-
'I' and 'P' in IPython are uppercase.
I am running the above command in python 3.7.
you need to import
from IPython.display import Image
initially, I imported
from IPython.display import image
so there is Image not image
For me, the problem (that drove me crazy) is that I actually needed capitalization. The correct import after pip install is:
from IPython.display import display, update_display
etc.

Python 2.7 can't find module Tkinter with the capital T

I am trying to run some plotting libraries that use Tkinter. I am using Python 2.7, and am getting the exact error (capitalization important :))
ImportError: No module named Tkinter
Seems like a lot of people are running into a similar issue that is solved by making sure to use the capitalized Tkinter versus all lowercase tkinter. I do not believe this is my problem since it is in fact capitalized.
When I run:
python --version
I get:
Python 2.7.5
Is there a place I can check to make sure Tkinter is properly installed? Has anyone else seen this particular issue with Python 2.7?
First, run pip freeze from the directory you want to use it. If you use a virtual environment, make sure that's active. This will show you all packages currently installed
The more exact option is to check the site-packages where they get installed (I use ipython):
How do I find the location of my Python site-packages directory?
$ ipython
In [1]: import site
In [2]: site.getsitepackages()
Out[2]:
['/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages',
'/Library/Python/3.6/site-packages']
then go there:
cchilders:/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
$ ls
IPython jupyter_client python_dateutil-2.6.0.dist-info
Jinja2-2.9.6.dist-info jupyter_client-5.0.1.dist-info pytz
MarkupSafe-1.0.dist-info jupyter_console pytz-2017.2.dist-info
Pygments-2.2.0.dist-info jupyter_console-5.1.0.dist-info pyzmq-16.0.2.dist-info
__pycache__ jupyter_core qtconsole
appnope jupyter_core-4.3.0.dist-info qtconsole-4.3.0.dist-info
...etc...
The same things you saw on pip freeze should show up here.
Make sure you have the package for the right version of python. If using python 3, you have to say:
pip3 freeze
But apparently the safest way to install is using apt if you have linux:
sudo apt-get install python-tk
Install tkinter for Python

Jupyter: install new modules

I have recently installed Anaconda with Python 3.5 and all the rest. I come from R where I am used to install packages dynamically. I am trying to install a module called scitools through jupyter notebook. I would like to recreate this in jupyter. However, I don't know how to dynamically install packages (if it's possible). I would greatly appreciate your help. Thank you!
EDIT: I am trying to use conda as recommended by the community, but it's not working. I am using mac OSX
Check Jake Vander Plus Blog here to learn how to install a package with pip from Jupyter Notebook.
# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install numpy
So if you have already done the install with anaconda, you may already have the module installed. In that case in your jupyter notebook after you have activated your kernel, you just need to make sure you execute the import statement.
import scitools
If you haven't installed that module yet, you can install it one of two ways. Both work from your command line or terminal.
pip install scitools
or since you have Anaconda
conda install scitools
and that should do it. Your import statement in your notebook when executed should correctly locate and enable the use of that module.
I had the same issue. It turns out if you open an anaconda window, which in Windows is accessible under the Anaconda drop down, it points to the correct location to install (or update) using pip.

`ipython` tab autocomplete does not work on imported module

Tab completion on IPython seems not to be working. For example,
import numpy
numpy.<tab>
simply adds a tab.
import numpy
num<tab>
just adds a tab, too. Could you please suggest some possible causes for this problem? I am running Windows 7 and Python 2.6.5.
Be sure you have installed the pyreadline library. It is needed for tab completion and other IPython functions - in Windows it doesn't come with the IPython package and you have to install it separately -
> pip install pyreadline
In case anyone is using the recent 7.19.0 and autocomplete does not work, try downgrading jedi to 0.17.2:
pip install jedi==0.17.2
See https://github.com/ipython/ipython/issues/12740 for details.
pip uninstall jedi --yes
and
pip install pyreadline
The current Ipython with the Jupyter notebook doesn't require jedi.. So you have to just uninstall it with the above command.
I got it from here.
pip told me I had pyreadline version 1.7.1 installed
C:\Users\me>pip freeze | grep readline
pyreadline==1.7.1
Upgrading pyreadline fixed it for me:
C:\Users\me>pip install --upgrade pyreadline
C:\Users\me>pip freeze | grep readline
pyreadline==2.0
Your ipythonrc file may be out of date.
Try running
ipython -upgrade
Downgrading iPython did the trick.
pip install --upgrade ipython==5.8.0
I had this problem.
I solved by downgrade the python-parso package
downgrading the python-parso package (0.8.0-1 => 0.6.2-1)
This should definitely work as it worked in my case
conda install ipython
pip install jedi==0.17.2
The classic 'have you tried turning it off and on again' worked for me.
pip uninstall ipython
pip install ipython
As of right now, on a OSX, pip installed ipython doesn't give tab completion, pyreadline release.py is busted .. what WFM:
easy_install ipython readline
YMMV.
Someone else in StackOverflow posted this link: http://www.vankouteren.eu/blog/2009/06/getting-ipython-readline-and-auto-completion-to-work-on-mac-os-x/
Its basicly easy_install readline than discover where the readline egg got installed and edit the ipython bin script to use this readline:
Install the "official" readline: easy_install readline
Discover where it is. Look at /Library/Python/site-packages/readline-*.egg or in your Virtualenv counterpart
Discover where ipython bin is: which ipython
Add ONE LINE to this file, adding the readline egg path right after import sys line.
My virtualenved ipython bin script got working as follow:
#!/Users/alanjds/src/git/cervejeiras/venv/cervejeiras-lfs/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'ipython==0.13.1','console_scripts','ipython'
__requires__ = 'ipython==0.13.1'
import sys
### ONLY LINE ADDED:
sys.path.insert(0, '/Users/alanjds/src/git/cervejeiras/venv/cervejeiras-lfs/lib/python2.6/site-packages/readline-6.2.4.1-py2.6-macosx-10.6-fat.egg')
####
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('ipython==0.13.1', 'console_scripts', 'ipython')()
)
I realize this is a really old question, but none of the answers above worked for me (And this is the first hit you get when you google a question of this nature).
I should mention that this is NOT exclusive to windows, I had the problem running CentOS 6.5 and Python 2.7
Here is what I did:
apt-get/yum install ncurses-devel
#If you want history in iPython:
apt-get/yum install sqlite-devel
easy_install ipython readline
ipython
In [1]: from
Display all 391 possibilities? (y or n)
If you don't have the -devel packages, your install will fail when it comes time to link them and build the eggs.. Hope this helps others!
I had this problem and knew that I had the pip installed for the module I was looking for. Performing $ ipython --init solved the problem for me.
I had to mv ~/.ipython{,.bak} in my case.
If you use Jupyter notebook and you still did get Tab auto-complete working after you tried all the steps suggested in the post here, you might want to check if you are trying to use the Tab auto-completion within a function definition. Ifyour import statements are part of the function such as below, you will not get the Tab auto-completion. You need to put the import statements outside the function and also execute them once before asking for auto-completion on the packages.
def myfunction():
import pandas as pd
import numpy as np
a = pd.DataFrame(np.random.normal(1,3, (4,4))
return a
I faced the same problem with the numpy library. The issue is with the particular version of ipython or jupyter notebook and it is resolved by simply updating ipython or jupyter.
If you are using a conda environment like anaconda or miniconda then update ipython in that environment by using
conda update ipython
In case of anaconda you also need to update the qtconsole
conda update qtconsole
Sometimes anaconda constraints the update of ipython then try
conda update -all
If you are not using a environment then directly update using pip
pip update ipython
I solved my issue by installing jedi-language-server:
pip install -U jedi-language-server
PS, I was installed Ipython from Conda in a virtual env and used the above command when the env was activated.
To check if ipython and the modules it uses match, run pip check ipython.
For my configuration with ipython 7.25 in July 2021, this gave a good clear warning:
ipykernel 6.0.1 has requirement importlib-metadata<4; python_version < "3.8.0", but you have importlib-metadata 4.6.0.
You may of course see different warnings --
this is just an example, unrelated to tab completion.
Then to downgrade it, e.g.
pip install 'importlib-metadata<4' # don't forget the 'quotes'
Successfully uninstalled importlib-metadata-4.6.0
Successfully installed importlib-metadata-3.10.1
Also useful:
pip list -- everything in your $PYTHONPATH, with version and location
pip check -- everything
pip show ipython --
Requires: traitlets, pygments, jedi, decorator, pickleshare, pexpect, appnope, matplotlib-inline, setuptools, prompt-toolkit, backcall
but to see the required versions you have to look at
.../site-packages/ipython-*.dist-info/METADATA
Pyreadline is needed by ipython. Install pyreadline. This was done in Windows 7. Get pyreadline zip, pyreadline-master.zip, unzip. In powershell change directory into uzipped pyreadline, make sure python is set in Path, and enter commandpython setup.py install This will intall pyreadline in C:\Python27\Lib\site-packages

Categories

Resources