I want to know how import a file that is one level up.
I read in google this:
>>> sys.path.append("../")
But I dont like it and i hope there are a better solution
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Another option, if what you are writing is part of a package, is to use relative imports like so:
where
foois the name of the module you’re trying to import.Similarly, if you’ve got another module in the current directory, you can use:
Sadly (or perhaps not so sadly), though, this doesn’t extend any further than this. You can’t go up to a grandparent, or higher.
EDIT:
As so graciously pointed out by JAB, my last comment there is not true:
I must have internalized Guido’s description of it too much. ;^)
EDIT:
Okay, I just verified this in 2.7 – this apparently goes as deep as you like, and is dependent on the number of
.works juuuust fine. I think I’m going to need a bucket