Forums

FileNotFoundError that only happens in deployed code

Hello friends.

I get the following error on trying to access a particular subpage of my flask web app:

2022-12-16 10:08:26,145: Exception on /ru [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.10/site-packages/flask/app.py", line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/relchron/relchron_1/app.py", line 16, in ru
    oldest_variety, newest_variety = get_abbr("data/examples_ru.csv")
  File "/home/relchron/relchron_1/app.py", line 154, in get_abbr
    with filepath.open(encoding="utf-8-sig", newline='') as infile:
  File "/usr/local/lib/python3.10/pathlib.py", line 1119, in open
    return self._accessor.open(self, mode, buffering, encoding, errors,
FileNotFoundError: [Errno 2] No such file or directory: 'data/examples_ru.csv'

This is the offending line of code:

filepath = Path("data/examples_ru.csv")
print("Filepath exists:", filepath.exists())

with filepath.open(encoding="utf-8-sig", newline='') as infile:
    [...]

I have also tried the following:

with open("data/examples_ru.csv", encoding="utf-8-sig", newline='') as infile:

The file "data/examples_ru.csv" is in the appropriate directory. Everything works when I run flask locally, it's just on pythonanywhere that it fails. Locally, the debug line with filepath.exists() returns True. On pythonanywhere I don't see the statement (not sure where to look).

Here's more complete context for the function calls (this is my app.py which sits at the root):

@app.route("/ru")
def ru():
    oldest_variety, newest_variety = get_abbr("data/examples_ru.csv")
    [...]

def get_abbr(examples_file_path):
    filepath = Path(examples_file_path)
    print("Filepath exists:", filepath.exists())

    with filepath.open(encoding="utf-8-sig", newline="") as infile:
        [...]

Here's some printouts about file paths from my console:

11:07 ~/relchron_1/data (main)$ ls
examples_ru.csv  examples_ru.json  sound_changes_hr.csv  sound_changes_hr.json  sound_changes_ru.csv  sound_changes_ru.json
11:07 ~/relchron_1/data (main)$ pwd
/home/relchron/relchron_1/data

11:08 ~/relchron_1 (main)$ ls
README.md  __pycache__  app.py  data  static  templates
11:08 ~/relchron_1 (main)$ pwd
/home/relchron/relchron_1

What is going wrong?

I'm also experiencing a similar issue; I'm trying to access "npm" via subprocess.call("npm ..." ...etc)

And only in deployed code, is it not able to find npm, and returns an error "npm: not found"

but when I manually run the same script within a web console, it works fine.

I resolved my issue, not sure if it will help you but here you go... I'm running a web server via flask. Turns out that the python code that is in my app.py (which is in /home/relchron/relchron_1) gets run in a different directory. It's running in /home/relchron rather than /home/relchron/relchron_1. So that's why it can't find /home/relchron/data/examples_ru.csv, because it's one step too far up the structure. I changed the path to /home/relchron/relchron_1/data/examples_ru.csv and now it works.

You can also build that absolute path from location of your code with Path(__file__).parent.resolve()

See https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/