-3

I'm beginning to learn python, but when I try to import modules from an ather file I get this error:

Traceback (most recent call last):
 File "./test", line 4, in <module>
  from multip import table
ImportError: No module named multip

The both files are in the same directory

when I import modules like 'math' or 'os' it's work, the probleme is between files

OS:ubuntu 12.04

python version:python 3.2.3

Learner
  • 33
  • 1
  • 6
  • Can you post the names of the source files in your directory? Do you have a file named multip.py? – svk Mar 24 '13 at 12:34
  • @user2204411 you'll need to post the directory layout, it seems your code should work unless there are some important details missing – wRAR Mar 24 '13 at 14:24
  • @wRAR/home/me/test... /home/me/multip... /home/me/_init_.py... drwxr-xr-x me.... with ubuntu I don't need .py right? – Learner Mar 24 '13 at 14:47
  • You don't need it for files you run but need for files you import. – wRAR Mar 24 '13 at 14:49
  • possible duplicate of [What is \_\_init\_\_.py for?](http://stackoverflow.com/questions/448271/what-is-init-py-for) – Andy Hayden Mar 24 '13 at 16:49

2 Answers2

0

You can import only files that have a .py extension. (or directories having a __init__.py file in them).

wRAR
  • 25,009
  • 4
  • 84
  • 97
  • I tried the two ways ,but nothing now I have /home/me/test.py & /home/me/multip.py & /home/me/_init_.py – Learner Mar 24 '13 at 15:25
  • @user2204411 with `from multip import table`? – wRAR Mar 24 '13 at 15:33
  • no with from .multip.py import table & with from .multip import table – Learner Mar 24 '13 at 15:38
  • ok,thanks guys and special thanks to @wRAR it's a pleasure to learn with you – Learner Mar 24 '13 at 16:22
  • I recapitulate:...wRAR said,"You can import only files that have a .py extension. (or directories having a __init__.py file in them)....You don't need it(.py) for files you run but need for files you import".....it works – Learner Mar 24 '13 at 16:40
-1

EDIT : I was not aware that modifying the PYTHONPATH environnement was considered as a bad practice. The reason given by @wRAR is that it has a permanent effect that can have uncontrolled side effects. You had better trying the first proposition (sys.path.append) to see if it can solve your problem. More about the sys.path.append vs PYTHONPATH can be found in this topic : PYTHONPATH vs. sys.path


Isn't it related to your PYTHONPATH environnement variable ? If you add '.' or the directory were you are working, I guess it should be ok

in your shell :

export PYTHONPATH=.:$PYTHONPATH python test.py

or (for test purpose, not to be used systematically) in your python file :

 import sys
 sys.path.append(".")
Community
  • 1
  • 1
Dvx
  • 289
  • 2
  • 10
  • It's usually wrong idea to modify PYTHONPATH even if it helps. And in this case it won't help. – wRAR Mar 24 '13 at 13:34
  • Can you please elaborate a little bit further ? I have not found any reference where adding a repository to the current path is considered as a bad practice. I agree that sys.path.append is a bad practice, taht's why I specified that it was only for test purpose, but not PYTHONPATH. – Dvx Mar 24 '13 at 14:30
  • Adding random directories to the global PYTHONPATH is wrong because it can lead to some subtle problems if you do not control what is added there. Changing PYTHONPATH in a shell wrapper that runs some program which for some reasons didn't install its modules to public paths is OK but can be replaced with modifying `sys.path` in the main script. – wRAR Mar 24 '13 at 14:41
  • ok it has to do with the permanent effect of the shell variable modification vs a non permanent effect of the sys.path.append. Thanks for the explanation, I will modify my comment. – Dvx Mar 24 '13 at 14:46
  • last point : I was wondering why the PYTHONPATH advice was not accurate, and I think the answer is as follow : if you have a closer look at where python is looking for the files it can include, it starts with the current path : http://docs.python.org/2/tutorial/modules.html – Dvx Mar 29 '13 at 07:26