I have created a folder in my home directory in ubuntu 12.04 and stord all the python files there. I have added path to my directory to pythonpath variable also. But it is not working. Earlier files were executed when they were in home directory but now they also do not get executed.
In Ubuntu Terminal manish@manish-laptop:~$ echo $PYTHONPATH /home/manish/project:
manish@manish-laptop:~$ ls -l /home/manish/project
total 24
-rw-rw-r-- 1 manish manish 140 May 31 00:07 Connection.py
-rw-rw-r-- 1 manish manish 122 May 29 11:29 Connection.py~
-rw-rw-r-- 1 manish manish 7150 May 31 00:07 Host.py
-rw-rw-r-- 1 manish manish 7132 May 30 23:30 Host.py~
`
Execution From Terminal:
>>> import sys
>>> sys.path
['', '/home/manish/project', '/home/manish', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
Execution from IDLE :
>>> import sys
>>> sys.path
['/usr/bin', '/home/manish/project', '/home/manish', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']
In Ubuntu Terminal:
>>> import Host
>>> obj = Host()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
And Host is python file containing functions and I want to run some of the functions in the host file, that is why I am trying to create a object.It contains one class and class name is same as file name.
I have also granted execute permission on files using chmod command.
File do execute if I change the path to folder 'project' using cd command. Here is what i do
manish@manish-laptop:~$ cd project
manish@manish-laptop:~/project$ python
Python 2.7.3 (default, Apr 20 2012, 22:44:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile('Host.py')
>>> obj = Host()
>>>
1 Answer 1
Since import Host doesn't give an error, the file is getting located, recognized and imported just fine - there is nothing wrong with your PYTHONPATH. The problem is that you're using the imported module wrong.
If you do import Host, you're not importing into the global namespace, you're creating a Host namespace which contains all the names from the Host.py file. So if the file Host.py contains a class named Host, or a function named some_function, or a global variable named SOME_GLOBAL, the way you would access them is this:
>>> import Host
>>> obj = Host.Host()
>>> result = Host.some_function(1)
>>> x = Host.SOME_GLOBAL
Does that work?
The reason you were getting that 'module' object is not callable error is that in this import style, Host is the name of the module, i.e. the object corresponding to the whole Host.py file - not the name of the Host class inside the Host.py file. The name of the Host class is Host.Host. Note that this means you can put multiple classes/functions/etc in one file, and they can be named whatever you want, they don't have to match the filename in any way.
Alternatively, if you want Host to refer to the class and not the module, you can import only specific names from the Host.py file directly into the global namespace, like this:
>>> from Host import Host, some_function, SOME_GLOBAL
>>> obj = Host()
>>> result = some_function(1)
>>> x = SOME_GLOBAL
Or, if you want to get all the names from Host.py in the global namespace, you can do this instead - but it's generally not a good idea, because you may accidentally overwrite some existing variables:
>>> from Host import *
>>> obj = Host()
>>> # etc
More information on differences between import styles:
As I just said, the from Host import * style is usually considered bad - you may overwrite some existing variables without realizing, especially if you do it multiple times from different files. It's probably acceptable if you're just playing around in interactive python (although often inconvenient, see next point), but using it in a script can make maintenance really annoying, since there's no quick way of checking whether a particular class/function that's being used in the code came from the Host module or somewhere else.
Also note that if you use the import Host format, you can make changes to the Host.py file and load them into your interactive python shell without exiting, with reload(Host) - if you use one of the from Host import styles, you won't be able to do that.
In any case, there's really no need to use execfile for this kind of thing.
More info:
- nice SO question on import styles
- python docs on import
- more readable info on import
Original part of the answer - just asking for more information:
First, before you do anything with python, do:
echo $PYTHONPATH
in the shell. Does it contain the /home/manish/project directory like you expect?
Next, post the output of
ls -l /home/manish/project
just so we can be sure it actually does contain the files you think it contains.
If everything is working correctly so far, start python. Don't mess around with execfile (why are you even doing that?), just do a straight import of one of the files in /home/manish/project, like this:
>>> import Host
(without the '.py' extension).
What happens? Please edit your question to include the output of all these (and comment on my answer so I get a notification, if you want).
More requests for information:
If import Host works without an error, try these commands, and again paste the output into the question:
>>> import sys
>>> sys.modules[Host.__name__].__file__
>>> obj = Host.Host()
The output of sys.modules[Host.__name__].__file__ should tell you what file python is reading when you do import Host - is it the file you think it should be reading, or some other one?
5 Comments
import statement wrong. Leave me a comment if the above doesn't work.import Host and then obj = Host.Host() like I suggested. Anyway, see the last "More requests for information:" section of my answer for what to try next.
PYTHONPATH=pythonfiles/foo.egg"PYTHONPATH. If you want to use a different path for your project, just create a symlink there.