1

I have the following structure:

AXBot:
 __init__.py
 bot.py
 util.py
 settings.py
 creator
 __init__.py
 xbot.py

The problem is that I cannot import the 'util' module in 'xbot.py' because python ends with 'ImportError: No module named util'... how can I solve?

PS: I am using the following code to import:

import util
import settings

Thank you.

iKlsR
2,6706 gold badges29 silver badges46 bronze badges
asked Jul 14, 2012 at 20:08
3
  • From which directory are you running the code and how do you start it? Commented Jul 14, 2012 at 20:22
  • There is no util module to import. Python does only look in the modules directory and in all directories in sys.path for modules to import, and there simply is no module util in the AXBot.creator package. Use import AXBot.settings as settings or from AXBot import settings Commented Jul 14, 2012 at 20:36
  • 1
    but none of the answers are the solution. Commented Jul 14, 2012 at 20:49

4 Answers 4

3

It seems you're trying to run xbot.py from within the creator folder.

This is the output I get with xbot.py containing import util:

C:\Users\Luke\Python stuff\AXBot\creator>xbot.py
Traceback (most recent call last):
 File "C:\Users\Luke\Python stuff\AXBot\creator\xbot.py", line 4, in <module>
 import util
ImportError: No module named util

This is the output I get with xbot.py containing from . import util

C:\Users\Luke\Python stuff\AXBot\creator>xbot.py
Traceback (most recent call last):
 File "C:\Users\Luke\Python stuff\AXBot\creator\xbot.py", line 3, in <module>
 from . import util
ValueError: Attempted relative import in non-package

I also get this latter error with from .. import util instead of from . import util.

If you're running xbot.py from the directory containing it, Python can't tell that it's being run inside a package hierarchy. It thinks xbot.py isn't inside a package.

I replaced the line that attempted to import util with from AXBot import util, moved up a couple of directories and ran xbot.py using Python's -m command-line switch, which tells Python to run a module specified by module name instead of filename. Note that when you use -m, you pass in the fully-qualified name of the module, including the package hierarchy, but you don't include the file extension .py, because that's not part of the name of the module:

C:\Users\Luke\Python stuff\AXBot\creator>cd ..\..
C:\Users\Luke\Python stuff>python -m AXBot.creator.xbot
1232

I got the same output if I used import AXBot.util as util instead of from AXBot import util.

(I don't have your code to run, so instead I put a variable in util.py and attempted to print its value from within xbot.py. The value of this variable was 1232.)

answered Jul 14, 2012 at 21:45
3
  • Thanks a lot! it works! I ran the script using -m option and passing the init module name. It worked. Commented Jul 14, 2012 at 22:45
  • Just a little problem: If I reference to files using only the file's name, the script cannot find that file because the working directory is not the same as script's location. How can I reference to files in script's directory? Commented Jul 14, 2012 at 22:52
  • @hkproj: in a Python script, __file__ contains the filename of the script running. Use os.path.dirname on that to get the directory your script running out of, and use os.path.join to append the names of your files to this directory. See docs.python.org/library/os.path.html. Commented Jul 15, 2012 at 7:55
2

Use relative importing

from . import util
from . import settings

I would recomment changing your folder hierarchy though, that looks cleaner to me. Also check your PYTHONPATH, it should normally work the way you did it.

References:

Try to stay away from sys.path hacks.

answered Jul 14, 2012 at 20:29
4
  • 1
    tried relative importing. Python throws 'ValueError: Attempted relative import in non-package' Commented Jul 14, 2012 at 20:43
  • I'm guessing you are running this from the AXBot/creator directory- that is, python xbot.py. Instead, use it as a package is meant to be used: run it on a script outside the AXBot directory, and do from AXBot.creator import xbot. Also, @tehmisvh: don't you mean from .. import utils? Commented Jul 14, 2012 at 21:25
  • 1
    @DavidRobinson: using .. doesn't work either. It gives the same error as in hkproj's comment. Commented Jul 14, 2012 at 21:27
  • Read the first part of my comment: You have to use a script outside the directory, and use xbot like from AXBot.creator import xbot. Running python xbot.py will always cause that problem. Commented Jul 14, 2012 at 21:38
1

I think the namespacing is incorrect. From xbot.py, try using this import command

from AXBot import util
from AXBot import settings
answered Jul 14, 2012 at 20:23
1
  • Are you getting an import error? Or is it just not working when you are calling functions. I need to see how you are calling the objects inside of util and settings. You might try import AXBot.util and import AXBot.settings Commented Jul 14, 2012 at 21:55
0

where you are is the most important question! according to that, you use the import. please update your question

answered Jul 14, 2012 at 20:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.