How to import modules into Jupyter notebook - python-2.7

So I am using anaconda and conda in a Windows OS.
And as you may know Jupyter gets installed automatically with Anaconda.
My python code runs normally when using the python command
python myfile.py
However when I try to run it through jupyter I receive the following error
ImportError Traceback (most recent call last)
<ipython-input-1-43605f892034> in <module>()
1 #!/usr/bin/env python
2 import os
----> 3 from gensim import corpora, models, similarities
4 from gensim.models import LsiModel
5 import logging
ImportError: No module named gensim
To troubleshoot I checked the following
Made sure to run jupyter notebook while enabling the activating the proper environment
Made sure the activated envrioment has the "gensim package" installed

So to solve this issue I need to install the missing library through jupyter itself
So I have to add and run the following command in a jupyter cell
pip install --upgrade gensim

Related

Installed python module in google cloud shell but python import is giving ImportError

I have tried installing scipy in Google Cloud Shell. The package is installed, but a python import is giving "ImportError: No module named scipy". Screenshot
I have problem only with scipy. Tensorflow and numpy are all working fine.
This problem should be similar to Installed packages disappeared in Google Cloud Shell.
The problem here is that scipy has not been installed. The process got killed at 99%. This is caused by how pip tries to install the package. I suspect that you're using a small VM instance which has memory limitations when pip tries to load the whole file to memory before installing it.
The solution is to install scipy with this command:
pip --no-cache-dir install scipy
Here you ask pip not to cache the file which should do the trick to install scipy on your Google Cloud VM. After the successful installation you should be able to import the scipy module as intended.

import unicodecsv fails in jupyter

I tried to run
import unicodecsv
within jupyter by running a .ipynb file.
It failed.
Then I installed the unicodecsv file through the python install command and found it within c\python27 dir. But still the import did not happen.
How should it be installed. Does it need to be placed within the anaconda installation
Edit :
Error displayed -
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-9c1521d8df38> in <module>()
2 # 1 #
3 #####################################
----> 4 import unicodecsv
5 ## Read in the data from daily_engagement.csv and project_submissions.csv
6 ## and store the results in the below variables.
ImportError: No module named unicodecsv
Pip (Package Installer for Python) package manager also comes with Python3, you can try this variant without installing Conda (Windows):
python -m pip install unicodecsv
I had the same problem for the same file at Udacity.
Try changing the Kernel from Python2 to Python3 in the Kernel -> Change Kernel menu.
Hope that helps :)
You should install it (from the command prompt) using:
conda install unicodecsv
Install unicodecsv using:
sudo pip install unicodecsv
Restart/run your notebook again.
I had the same issue. Switching to Python 3 requires you to learn the new syntax and the rest of the nanodegree uses Python 2.
Make sure you are running the notebook in the same environment you are installing the the unicodecsv package in. Then:
conda install unicodecsv
should work. It did for me.

How to import psycopg2 when installed - Windows

Can somebody please help me install this psycopg2 which I dont even know what it is. I have no python knowledge, I need this to run a code to connect to read data from txt to postgres.
Problem is, I have installed psycopg2 from exe and but when I run import psycopg2 in python shell I get an error:
import psycopg2
Traceback (most recent call last): File "", line 1, in
import psycopg2 ImportError: No module named psycopg2
i think Psycopg2 is not installed.
please install psycopg2 using pip
like:
pip install psycopg2
once done with this you can import
the pip install in windows doesn't seem to work well and ends up with the error you have mentioned. You can try the binaries provided in StickPeople website. These binaries install without problem and will allow you to access psycopg2. Please check the version of python you have installed with the version of the psycopg2 version. If they don't match up, it'll result in such errors. Even the architecture x86 or x64 matters.
C:\> easy_install psycopg2-2.6.1.win32-py2.7-pg9.4.4-release.exe, you can try this if the binary fails.

GraphLab Create "ImportError: No module named graphlab"

I followed these instructions to set up GraphLab on my Ubuntu machine. At the end, I opened Python 2.7.6 and ran the first of the test lines import graphlab as gl. This gave me
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named graphlab
How can I begin to diagnose this?
Details:
I ran python -V from a terminal, and it returned me Python 2.7.6.
In /usr/bin I find the following pyth* entries ... I wonder if something somewhere pointed at the wrong version:
python python2.7-config python3.4 python-config
python2 python2-config python3.4m pythontex
python2.7 python3 python3m pythontex3
The Dato Graphlab Create installer did not actually install graphlab on my Mac (El Capitan). I did the following (Anaconda is installed) in a terminal window:
% pip install graphlab-create
That subsequently installed Graphlab Create. You can then easily verify:
% python
Python 2.7.10 |Continuum Analytics, Inc.| (default, Sep 15 2015, 14:29:08)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import graphlab
>>>
I've noticed that occasionally, Python will forget that Graphlab Create is installed. A repeat of the above 'pip' command will cause it to remember.
python anaconda graphlab
Another option is to use virtualenvwrapper for the easy creation and application of virtual environments. For example, following this documentation, start with installation:
sudo pip install virtualenvwrapper
Open your .bashrc settings file, for example run gedit .bashrc and append the following lines to the bottom of it:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
Restart your terminal window, and then you can make your virtual environment, say call it "test":
mkvirtualenv test
Now test is a virtual environment, and your are in it (i.e., test is "activated" currently). To put GraphLab in test,
pip install graphlab-create
Similarly, you can install other python toolkits in test by using pip, and any python program you run from within test will be able to see only the python toolkits that are installed here.
Maybe you should install graphlab in virtualenv.
1.Ensure your system has virtualenv installed. To verify, execute pip freeze. To install, execute sudo pip install virtualenv in your terminal before proceeding
2.Copy and execute the following commands in your terminal. This will create a virtual environment called 'graphlab' and install graphlab create version 0.9.1
virtualenv graphlab
. graphlab/bin/activate
pip install graphlab-create==0.9.1
You may need to activate the conda env by running
source activate dato-env
inside the terminal
Check your system path
import sys
print sys.path
It should contain graphlab-0.9.1. If not, then something was odd with our installation. I recommend using a virtual environment in python.
I had the same problem on ubuntu 16 desktop. The solution for me was pretty simple. After you start the notebook using
(gl-env) davis#smeagol:~/progs/ml-foundations$ jupyter notebook
Click the file navigator to locate your notebook where you do the import graphlab which causes the error. When it starts the notebook I imagine you see |Python [Root] in the top right. To fix this, click the title bar Kernel->Change kernel->gl-env. Now the top right label should say |Python [gl-env]. Afterwards when you run the notebook import graphlab will work.
There is a tab on the intial landing page of the Jupyter UI which has Conda. In that you can see two env's named root and gl-env. I've tried to delete the root one and even though its not the default all my notebooks start up with that environment and deleting it causes an internal error.
Graphlab is not supported on python3. Install Python 2.7 as mentioned in
https://conda.io/docs/user-guide/tasks/manage-python.html
If you don't see graphlab, simply the path of the environment is not set on "dato-env" (rather it may be set on "root")
If you use "Launcher" application, on top left set "Environment" to "dato-env".
Well,I guess the thread is dead.
After tinkering w/ un/reinstallations a couple times, the only way I can get "import graphlab" to work reliably is to manually activate dato-env.
Open your terminal and type below command
source activate dato-env
Prior to this close all the jupyter notebook. I ascertain that dato-env is in effect when my bash prompt changes to: (dato-env) pydev#smruti:~$
Now on your Jupyter notebook try to do import graphlab,this will execute without showing import error.
Hope this helps!!
I had the same problems, but then I found that in the files that come along with the Machine Learning specialization (https://www.coursera.org/learn/ml-foundations/notebook/lGQH5/open-your-notebook-workspace-to-follow-along) there are some additional codes after which you don't get any errors:
import graphlab
Set product key on this computer. After running this cell, you will not need to re-enter your product key.
graphlab.product_key.set_product_key('your product key here')
Limit number of worker processes. This preserves system memory, which prevents hosted notebooks from crashing.
graphlab.set_runtime_config('GRAPHLAB_DEFAULT_NUM_PYLAMBDA_WORKERS', 4)
Output active product key.
graphlab.product_key.get_product_key()
I had the same problem. I follow these steps.
1.Install Anaconda 2.7 version. Then I created vitual environment and selected python 2.7 version.
2.After create virtual environment open terminal in and run pip install notebook.
3.Then I registered https://turi.com/ because Graphlab Create requires an academic license to use.Run the following command that is given by after registration in terminal.
pip install --upgrade --no-cache-dir https://get.graphlab.com/GraphLab-Create/2.1/your registered email address here/your product key here/GraphLab-Create-License.tar.gz
4.Run jupyter notebook.
5.import graphlab
6.Then I got an error.So i run graphlab.get_dependancies() command. After that restarted the kernel.
7.After above all steps I typed import graphlab again.
8.It executed without errors.

Installing a Module on Heroku

I'm deploying something that has been running on my local and realized when I deployed that the module I was using wasn't installed on Heroku, thus I was getting an error like this:
...
from PIL import Image
ImportError: No module named PIL
I've tried:
heroku run pip install PIL
but I'm getting this:
ImportError: No module named setuptools.command
edit
So, I went to the heroku setup and mimicked the steps to install django and postgres. Essentially, I activated the environment and then ran
pip install PIL
This seemed to be doing the trick, I got a lot of readout and then it ended with a confirmation that PIL had been installed. But then again, it said it couldn't find PIL when I ran the new file.
Every command you run on Heroku is run in an isolated and ephemeral environment — any changes you make during heroku run are thrown away immediately when the processes completes.
To make PIL available to your application, you need to add it to requirements.txt instead.
Sometimes PIL package is added in your path but it's somewhere else than in site-packages. In this case you will be able to just import Image.
To make sure try something like this:
>>> import sys
>>> sys.path
[(...), '/usr/lib/python2.7/dist-packages/PIL', (...)]
>>> from PIL import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL
>>> import Image
>>> Image
<module 'Image' from '/usr/lib/python2.7/dist-packages/PIL/Image.pyc'>
In my applications I use code like this:
try:
import Image, ImageDraw, ImageFont
except:
from PIL import Image, ImageDraw, ImageFont
Are you sure you have followed http://devcenter.heroku.com/articles/django#prerequisites and your virtualenv is loaded?
BTW, I recommend using Pillow instead of PIL.
Introduction
The fork author's goal is to foster packaging improvements via:
Publicized development and solicitation of community support.
Exploration of packaging problems within the fork, most noticably via
adding setuptools support but also via clean up & refactoring of
packaging code. Why a fork?
PIL is currently not setuptools compatible. Please see
http://mail.python.org/pipermail/image-sig/2010-August/006480.html for
a more detailed explanation. Also, PIL's current release/maintenance
schedule is not compatible with the various & frequent packaging
issues that have occured.
Even i was facing the same issue, I spent quite a lot of time.
I tried this
heroku run pip install PIL --app=your-app
error:
Running `pip install PIL` attached to terminal... up, run.1983
Downloading/unpacking PIL
Could not find any downloads that satisfy the requirement PIL
Some externally hosted files were ignored (use --allow-external PIL to allow).
Cleaning up...
No distributions at all found for PIL
Storing debug log for failure in /app/.pip/pip.log
But this helps like a charm :)
heroku run pip install Pillow --app=your-app