13

I have installed ubuntu on my laptop and i have installed python, after installing python2.7.5 i was trying to run a python script on terminal, but it said module no found, i started to download all the modules but it still said module not found. After upgrading to python2.7.9 it still said same so i installed python iddle shell which is importing the modules correctly.

Why is it happening ? why is it working on the python shell but not on terminal. terminal is only recognizing modules like sys, os.. and some built-in modules but not the installed. I would appreciate the help. (I just started to use linux)

enter image description here

9
  • Please show your output and related code. Commented May 21, 2015 at 15:33
  • At a guess your PATH or PYTHONPATH environment variables are different. Commented May 21, 2015 at 15:37
  • how can i change the paths? i am new on linux Commented May 21, 2015 at 15:38
  • Do you start the python shell by simply calling python or from a menu item of your desktop. If you start it from the menu it is possible, that it start a different installation. Commented May 21, 2015 at 15:39
  • The picture does not show the version of python in the command line. Could you compare the versions. Commented May 21, 2015 at 15:41

2 Answers 2

13

It seems that your Python shell uses a diffenrent PYTHONPATH than the python you execute in the terminal. You can verify that by typing

import sys
print sys.path

in both shells and comparing the two outputs. I assume that the installed module path(s) are missing in the output of the python started in the terminal.

you can solve this by defining a PYTHONPATH in your shell:

export PYTHONPATH=...

... means all paths of the python shell's output separated by :

Don't use spaces. If there spaces in one of the paths, surround ... with quotes

export PYTHONPATH="path with spaces:other path:path"

Start python from that terminal where you entered the export command. Try to import your modules. If it works, make the export permanent by appending it in your .profile located in your home directory.

ls -a $HOME 

shows the file (and many others ;-). It is a .file. .files are hidden on a simple ls.

Sign up to request clarification or add additional context in comments.

4 Comments

when i type echo $PYTHONPATH on terminal it shows /usr/bin/python , i guess this is the path that the python shell is using, but how can i check the one that the terminal is using? when i type` whereis python` it shows python: /usr/bin/python3.4m /usr/bin/python3.4 /usr/bin/python2.7-config /usr/bin/python2.7 /usr/bin/python /usr/lib/python3.4 /usr/lib/python2.7 /etc/python3.4 /etc/python2.7 /etc/python /usr/local/bin/python2.7-config /usr/local/bin/python2.7 /usr/local/bin/python /usr/local/lib/python3.4 /usr/local/lib/python2.7 /usr/include/python3.4m /usr/incl...
they both are in different path you are right, now how i am a bit confusing in changing the path
Use the output of your print sys.path of the python shell (which works) to build a PYTHONPATH shell variable. if the output is p1 p2 p3 p4 then enter export PYTHONPATH=p1:p2:p3:p4 into the shell where it does not work. After that start python in this shell. Then try to import the missing modules again. If it works, you are done and you can enter the export command into your .profile to make it permanent, i.e. PYTHONPATH will be setup every time you log in. I described this in my answer.
thanks peter it helped but now it is not recognizing just some libraries and i have exported the libraries that are in the shell that sys prints
0

Try to install python again. follow the steps.

installing dependencies:

sudo apt-get install build-essential
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev  libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

then download python version you want.

cd ~/Downloads/
wget http://python.org/ftp/python/2.7.9/Python-2.7.9.tgz

tar -xvf Python-2.7.9.tgz
cd Python-2.7.9

after extract files

./configure
make
sudo make install

2 Comments

Thank you @aronadaal. my mistake. confused with ":~$" and ">"
thanks, but i did it before asking this question so it wouldnt help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.