0

Can I import the test01.py like os, sys module?

I want to import the test01.py like:

import test01.py

In this case now, I only can import it like this:

from testDemo02 import test01

Is it possible to reach it?

Shadow
  • 8,749
  • 4
  • 47
  • 57
1243916142
  • 365
  • 6
  • 17

2 Answers2

1

It looks like test01 is in the package testDemo02 - you can tell because there is a file __init__.py in the directory testDemo02. Given that, there are a couple possibilities:

  • If the parent directory of testDemo02 is in the module search path (sys.path), but testDemo02 itself is not, you can import your test01 module using either

    import testDemo02.test01
    

    or

    from testDemo02 import test01
    

    I suspect this is the case since you tried the latter one and it works. This is what I would expect because I see that __init__.py file there.

  • If testDemo02 itself is in the search path, you will be able to import your module with just

    import test01
    

    I would find it strange for a directory to be in the search path when it also contains an __init__.py file, but it is possible.

David Z
  • 128,184
  • 27
  • 255
  • 279
0

You can use sys module's path property to append your path:

>>> import sys
>>> sys.path.append("/testDemo02/test01")
>>> import test01
aircraft
  • 25,146
  • 28
  • 91
  • 166