How can I import files in Python using sys.path.append?
Asked Answered
C

2

30

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 doesn't make any 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?

Composition answered 27/8, 2015 at 1:43 Comment(9)
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.Hadfield
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?Composition
@Hunle use the complete relative path to that file , or best use absolute path if possible.Upthrow
Can't use absolute path. This is a shared application. Okay, thank you.Composition
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.Composition
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.Hadfield
similar question ----> [#714563 let me know if it helps. [1]: #714563Expectation
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.Wacky
sorry about that. suggest an edit to address this issue.Composition
H
20

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.

Hadfield answered 27/8, 2015 at 1:58 Comment(2)
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.Hadfield
I figured it out. Thanks for your help.Composition
I
5

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 directory one up from myfile.txt... so it is not ideal.

If you know that my_file.txt is always going to be in the same directory 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)
Iodous answered 27/8, 2015 at 2:1 Comment(1)
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.Iodous

© 2022 - 2024 — McMap. All rights reserved.