开发者

How to search the correct directory for imports

I am trying to test some code. The main script requires imports from a number of subdirectories. The structure of the scripts is like this (I edited it to make it clear that dir1 and 2 are subdirectories of build):

build
   ascript.py
  dir1
    script2.py
  dir2
    script3.py
    subdir1
      script4.py
      script5.py
    subdir2
       script6.py
.        
.
.

Note: Not a complete representation.

I was told to test SCRIPT2 through command shell, change to the build directory and then type:

SET PYTHONPATH="." python dir1/script2.py

That script one has an import statement:

from dir2.script3 import *

Script3 calls an import from scripts that are in one or more of the dir or subdir folders

So when run the command SET . . . (see above) I get no output. Thinking that this is a me problem not a code problem I copied dir2 to

C:\PROGRAM FILES\python264

ran the same instructions at the CMD prompt and I got some partial output

This tells me that the SET PYTHONPATH is not working as expected in Windows XP.

I hope this question makes sense.

In response to SLOTT's request - however, after reading his ques开发者_如何学Gotion I understood that one problem is that I did not understand that I was in fact submitting two commands

Note there is a script called node in the directory named html which is a subdirectory of parsers2 which is a subdirectory of core which is at the same level as exp

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\burchkealey.UNO_CBA>cd c:\

C:\>cd c:\texttool1\build

C:\texttool1\build>set pythonpath="."

C:\texttool1\build>python exp/extract_section.py c:\testextract\c40545.htm
Traceback (most recent call last):
  File "exp/extract_section.py", line 4, in <module>
    from  core.parsers2.html.node import *
 ImportError: No module named core.parsers2.html.node

C:\texttool1\build>


The commands set PYTHONPATH=C:\texttool1\build and dir1\script2.py should work perfectly. Make sure you

  • type them as two commands in the same console (or in one batch script)
  • use the absolute path to the folder containing the modules

Moreover, executable scripts are often written in a way that they must be executed from the directory in which the executable lies. Try to start "extract_section.py" in its directory instead of from the parent folder.

By the way, it's unusual to name a top package "core", but the code does a global import from that package (from core.parsers2.html.node import *). Or is "core" contained in another package? Maybe this should rather be a local import (from .core.parsers2.html.node import *)?


I haven't had much luck with PYTHONPATH on XP either. You may need to give a relative path for your include, or you can add the directory to the syspath:

sys.path.append('biglongdirectory')

It's strongly discouraged, though... instead, there's this option

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜