How to import files in python using sys.path.append?
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)
Related videos on Youtube
![makansij](https://i.stack.imgur.com/MpAe0.png?s=256&g=1)
makansij
I'm a PhD Student at University of Southern California.
Updated on August 27, 2020Comments
-
makansij almost 4 years
There are two directories on my desktop,
DIR1
andDIR2
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
intoDIR2
and runfile2.py
and it runs no problem.Scenario 2
I
cd
intoDIR1
and runfile1.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 commandsys.path.append('../DIR2')
.Why does this happen when
file1.py
, whenfile2.py
is in the same directory asmyfile.txt
yet it throws an error? Thank you.-
larsks almost 9 yearsPython's
sys.path
only affects how Python looks for modules. If you want toopen
a file,sys.path
is not involved. Youropen
is failing because you're not running the script from the directory that containsmyfile.txt
. -
makansij almost 9 yearsOkay, 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 almost 9 years@Hunle use the complete relative path to that file , or best use absolute path if possible.
-
makansij almost 9 yearsCan't use absolute path. This is a shared application. Okay, thank you.
-
makansij almost 9 yearsStill....I'm confused.
myfile.txt
is in the same directory as the file trying to open itfile2.py
. And, like I said, if I just runfile2.py
alone it works fine. -
larsks almost 9 yearsYou never showed us how you are running the files. If you're changing into
DIR2
before runningfile2
that would explain the behavior you are seeing. If you're doing anything else, show us the exact steps. -
Stef g almost 9 yearssimilar question ----> [stackoverflow.com/questions/714063/… let me know if it helps. [1]: stackoverflow.com/questions/714063/…
-
JoseOrtiz3 over 2 yearsThe title of this question is misleading, since it's about
open
, notimport
. People searching on Google for how to import usingsys.path.append()
will find this post a waste of time - and that's where most of the traffic is probably coming from. -
makansij over 2 yearssorry about that. suggest an edit to address this issue.
-
-
demented hedgehog almost 9 yearsCan 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 almost 9 yearsI 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 almost 9 yearsI figured it out. Thanks for your help.