4

I have recently switched from python 2.7 to python 3.2

considering following folder structure:

~/my_program
~/my_program/modules

where *my_program* is the root of the application, containing main script called main.py

  *my_program/modules* is used to store all additional classes and subscripts

in python2.x I was able to import any file "module" the same way I do import standard modules

from modules import abc.py 

however, while I try to launch the same program in python3.2 I get the error message saying:

File "/my_program/modules/__init__.py" line 1
import abc, def

ImportError: No module named abc

Please advise

m1k3y3
  • 2,762
  • 8
  • 39
  • 68

2 Answers2

3

It's a good example you are using, because "abc" is in fact a module in the standard library. So in fact, if you in Python 3 do import abc that will work just fine, but you will get the standard library module.

In Python 2, if you had a local module called abc.py, that import abc would import the local module instead. That means you couldn't import the module from the standard library.

In Python 3, this has been changed, and you now need to use the fully qualified name, in your case probably from modules import abc.

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • I think I am still doing something wrong, I have changed the import line however does not seem to work. Error is the same "No module named abc", I tired changing __init__.py inside modules folder to reflect changes per your advise so the import line looks "import .abc" but it results in SyntaxError pointing to the "." in fornt of abc. – m1k3y3 Dec 14 '11 at 22:25
  • Yes, you are right, I misremembered. Relative import syntax is `from .abc import blah` you can't just do `import .abc`. In this case you therefore need to do a non-relative import and use the fully module path: This would be "from modules import abc" as the root module is called modules, if I understand you correctly. – Lennart Regebro Dec 15 '11 at 09:32
  • I think it seems to be better, however "ValueError: Attempted relative import in non-package" – m1k3y3 Dec 16 '11 at 15:53
  • 1
    ok, I made more research, came across your other post (http://stackoverflow.com/questions/1581260/how-do-i-make-these-relative-imports-work-in-python-3) and finally figured it out. – m1k3y3 Dec 16 '11 at 19:57
0

Where are you storing the module you created? When I create a module, I do the following:

Create a folder in the lib\sitepackages folder in your python folder ex: myModules Save a blank python file named init.py in this folder. The file does not have to contain any syntax. Later on, you can set module requirements in this file if you wish. Save your module into this folder ex: myGamesModule.py In idle (or whichever python connected IDE you use) type:

from myModules import myGamesModule or from myModules import myGamesModule as myGmMod

That should import your module and then you can call the classes etc ex myGmMod.Player()

I am sure that this is correct, as I have just done it and it was ok.

I have found that sometimes, if I create a blank text file in the module folder and rename it to init.py, it has caused me problems before. I usually just open IDLE and save a init.py file from there into my module folder and then use whichever IDE I use (sublime) to create and save the rest of the files.

Also, for some reason, the text box has disallowed the underscores I am using in this text so where you see init.py, it should be (underscoreunderscore*init*underscoreunderscore.py without any asterix

EMJAY
  • 5
  • 7