0

I have a django project that worked perfectly on my local server returning a response. I am now trying to run it on pythonanywhere, it keeps saying no such directory or file. I initially used the os.path.dirname("__file__") but then I changed it into the absolute address, i.e. "/home/username/projectname/filename" to no avail. That latter method is the only one others on the web are suggesting, but it still isn't working. Is there a special syntax to access files in pythonanywhere? or do you have any suggestions? Thanks.

The following is the line that throws the error:

with open("home/<username>/<project>/layer.pem", "r") as rsa_priv_file:

Directory structure: Directory structure

4
  • 1
    are you entering your actual username and project in the fields <username> and <project>? Commented Jun 30, 2015 at 5:09
  • 1
    you must have seen so much in this world :D, yes I am entering the real names. Commented Jun 30, 2015 at 5:10
  • Can you show your app's directory and file structure? Commented Jun 30, 2015 at 5:25
  • I just did, sorry I had to redact parts of it. Commented Jun 30, 2015 at 6:23

1 Answer 1

3

If this with open("home/<username>/<project>/layer.pem", "r") as rsa_priv_file: is the actual code you're using, then you're missing a / at the beginning. What you're actually asking for with that code is not the absolute path to layer.pem, but a relative path rooted in the current directory.

Also, the os.path.dirname("__file__") is not working because you quoted the __file__. What you're asking for is the dirname of a file called "__file__" (which will be an empty string), not the dirname of the current file.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Glenn, I will try, but in the file case, it wouldnt work unless i quoted it on my local server.
It may "work" in the sense of not raising an exception, but it's not doing anything close to what you expect.
Correct, but i made sure im getting a return value, ie i checked it outside the function on a console. No quotes threw the undefined variable error anf with quotes it opened the file. May it be jupyter? Or windows? I am using 2.7.
Ah. In Jupyter, __file__ is not defined. I'm not sure why they decided not to set it, but I guess there's a good reason.
Interesting! Thank you so much for your help, i will try your guidelines tonight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.