How to import files in python using sys.path.append?

148,622

Solution 1

You can create a path relative to a module by using a module's __file__ attribute. For example:

myfile = open(os.path.join(
    os.path.dirname(__file__),
    MY_FILE))

This should do what you want regardless of where you start your script.

Solution 2

Replace

MY_FILE = "myfile.txt"
myfile = open(MY_FILE) 

with

MY_FILE = os.path.join("DIR2", "myfile.txt")
myfile = open(MY_FILE) 

That's what the comments your question has are referring to as the relative path solution. This assumes that you're running it from the dir one up from myfile.txt... so not ideal.

If you know that my_file.txt is always going to be in the same dir as file2.py then you can try something like this in file2..

from os import path

fname =  path.abspath(path.join(path.dirname(__file__), "my_file.txt"))
myfile = open(fname)
Share:
148,622

Related videos on Youtube

makansij
Author by

makansij

I'm a PhD Student at University of Southern California.

Updated on August 27, 2020

Comments

  • makansij
    makansij almost 4 years

    There are two directories on my desktop, DIR1 and DIR2 which contain the following files:

    DIR1:
    file1.py
    
    DIR2:
    file2.py  myfile.txt
    

    The files contain the following:

    file1.py

    import sys
    
    sys.path.append('.')
    sys.path.append('../DIR2')
    
    import file2
    

    file2.py

    import sys
    
    sys.path.append( '.' )
    sys.path.append( '../DIR2' )
    
    MY_FILE = "myfile.txt"
    
    myfile = open(MY_FILE) 
    

    myfile.txt

    some text
    

    Now, there are two scenarios. The first works, the second gives an error.

    Scenario 1

    I cd into DIR2 and run file2.py and it runs no problem.

    Scenario 2

    I cd into DIR1 and run file1.py and it throws an error:

    Traceback (most recent call last):
      File "<absolute-path>/DIR1/file1.py", line 6, in <module>
        import file2
      File "../DIR2/file2.py", line 9, in <module>
        myfile = open(MY_FILE)
    IOError: [Errno 2] No such file or directory: 'myfile.txt'
    

    However, this makes no sense to me, since I have appended the path to file1.py using the command sys.path.append('../DIR2').

    Why does this happen when file1.py, when file2.py is in the same directory as myfile.txt yet it throws an error? Thank you.

    • larsks
      larsks almost 9 years
      Python's sys.path only affects how Python looks for modules. If you want to open a file, sys.path is not involved. Your open is failing because you're not running the script from the directory that contains myfile.txt.
    • makansij
      makansij almost 9 years
      Okay, thanks @larsks. But, how do I affect how python opens files? i.e. how do I allow it to open files from a different directory?
    • Anand S Kumar
      Anand S Kumar almost 9 years
      @Hunle use the complete relative path to that file , or best use absolute path if possible.
    • makansij
      makansij almost 9 years
      Can't use absolute path. This is a shared application. Okay, thank you.
    • makansij
      makansij almost 9 years
      Still....I'm confused. myfile.txt is in the same directory as the file trying to open it file2.py. And, like I said, if I just run file2.py alone it works fine.
    • larsks
      larsks almost 9 years
      You never showed us how you are running the files. If you're changing into DIR2 before running file2 that would explain the behavior you are seeing. If you're doing anything else, show us the exact steps.
    • Stef g
      Stef g almost 9 years
      similar question ----> [stackoverflow.com/questions/714063/… let me know if it helps. [1]: stackoverflow.com/questions/714063/…
    • JoseOrtiz3
      JoseOrtiz3 over 2 years
      The title of this question is misleading, since it's about open, not import. People searching on Google for how to import using sys.path.append() will find this post a waste of time - and that's where most of the traffic is probably coming from.
    • makansij
      makansij over 2 years
      sorry about that. suggest an edit to address this issue.
  • demented hedgehog
    demented hedgehog almost 9 years
    Can I suggest that you put a print(__file__ + " " + os.getcwd()) call in each of your files .. I think you may have a broken understanding of how the paths are resolved.
  • larsks
    larsks almost 9 years
    I would start by printing out the value of the path (thepath = os.path.join( os.path.dirname(__file__)+'/../<different-directory>/', JSON_FILE)) and seeing if it looks sane. I'm suspicious that we're dealing with an XY problem here, because it's not clear from your question exactly why you're trying to access files like this.
  • makansij
    makansij almost 9 years
    I figured it out. Thanks for your help.