1

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()
  >>> 
Manish
  • 71
  • 3
  • 10
  • Please describe what you did by actually giving code samples in your question. For example, you might write: "I added path to my directory to pythonpath variable like this: `PYTHONPATH=pythonfiles/foo.egg`" – kojiro May 30 '12 at 22:43
  • I added export PYTHONPATH=/home/manish/project:$PYTHONPATH to the .bashrc file and .profile files – Manish May 30 '12 at 22:48
  • i am executing the following lines >>> execfile('Host.py') >>> obj = Host() Traceback (most recent call last): File "", line 1, in NameError: name 'Host' is not defined – Manish May 30 '12 at 22:53
  • I would recommend using the [user site directory](http://docs.python.org/install/index.html#alternate-installation-the-user-scheme) for placing custom packages there, which can also be [used by pip](http://www.pip-installer.org/en/latest/other-tools.html) to install packages there. It's a lot better then to mess around with the `PYTHONPATH`. If you want to use a different path for your project, just create a symlink there. – mata May 30 '12 at 23:25
  • Please edit your question to include the information you put in the comments, so that it's formatted properly. Hard to read the traceback all squashed into a single line. – weronika May 31 '12 at 05:11
  • Question is edited , please have a look. – Manish May 31 '12 at 08:29
  • Another note: if the important part of the code in the Host.py file is the functions, there's no need to wrap them in a class - it may be better to just put the functions on the top level of the Host.py file. Of course, if the class is actually important, keep it. But I know some languages require everything to be wrapped in a class whether it makes sense or not, and this is bad style in python. – weronika May 31 '12 at 16:32

1 Answers1

2

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?

Community
  • 1
  • 1
weronika
  • 2,561
  • 2
  • 24
  • 30
  • @Manish - answer edited to hopefully explain what's going on. Spoiler: your PYTHONPATH is working fine, you're just using the `import` statement wrong. Leave me a comment if the above doesn't work. – weronika May 31 '12 at 16:10
  • Hey weronika , I do accept that its not even a week since i am using it.But the way you told me to import modules was already tried by me yesterday and it did not work. And actually I was using execfile() to execute host.I think you should see the complete question.Anyway thanks for trying but still it would be really helpful if you can find me a solution for it. – Manish May 31 '12 at 20:00
  • @Manish - how am I supposed to know what you tried yesterday? Nowhere in your question does it say that you tried doing `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. – weronika May 31 '12 at 20:37
  • Thank you very much for your efforts.I dont know how and why but it did work with the same syntax -- "from Host import Host" which I used earlier.I was showing the problem to research assistant and it worked then , making me fool in front of him. Thank You very much. – Manish Jun 01 '12 at 20:39
  • @Manish - Ah, the usual "things suddenly work when showing them to someone" effect. ;) Yeah, that's pretty common. Glad it's working now! – weronika Jun 01 '12 at 22:46