Forums

works on localhost but not on pythonanywhere

hello and I hope this email finds you well given the health crisis! I have a very strange problem. I have a function, extractImages(folder) which returns a list of image:

def extractImages(folder):
# variable to store links to the images in the folder
images = []
if os.path.exists(folder):
    entries = os.scandir(folder)
    for entry in entries:
        #print(entry)
        if entry.is_file():
            filename = entry.name
            if allowed_file(filename):
                filepath = os.path.join("/" + folder, filename)
                images.append(os.path.normpath(filepath))
return images

The have the home endpoint that calls this function:

@app.route('/', methods=['GET'])
@app.route('/index', methods=['GET'])
@app.route('/home', methods=['GET'])
def home():
    images = {
        'carousel': extractImages(CAROUSEL_FOLDER),
        'gallery': extractImages(GALLERY_FOLDER)
    }
    return render_template('home.html', title='Home', images=images)

The template:

{% block content %}
<p>Carousel images: {{ images['carousel'] }}</p>
<p>Gallery images: {{ images['gallery'] }}</p>

<img src="{{ images['gallery'][0] }}" \>

{% endblock content %}

On localhost, this works great: <p>Carousel images: ['\static\images\carousel\slide-1.jpg', '\static\images\carousel\slide-2.jpg', '\static\images\carousel\slide-3.jpg', '\static\images\carousel\slide-4.jpg']</p> <p>Gallery images: ['\static\images\gallery\1.jfif', '\static\images\gallery\10.jfif', '\static\images\gallery\11.jfif', '\static\images\gallery\12.jfif', '\static\images\gallery\13.jfif', '\static\images\gallery\14.jfif', '\static\images\gallery\15.jfif', '\static\images\gallery\16.jfif', '\static\images\gallery\2.jfif', '\static\images\gallery\3.jfif', '\static\images\gallery\4.jfif', '\static\images\gallery\5.jfif', '\static\images\gallery\6.jfif', '\static\images\gallery\7.jfif', '\static\images\gallery\8.jfif', '\static\images\gallery\9.jfif']</p>

<p>But when I deploy it to pythonanywhere, the images dictionary which i passed to the template is empty!!! Please help!</p>

You're probably using relative paths without paying attention to what the working directory is. See http://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/ for more details.

I doubt this is the problem because the extractImages() function does return the same list of links when I run it from PYTHONANYWHERE command line:

    extractImages(CAROUSEL_FOLDER) * Serving Flask app "__main__" (lazy loading)
   * Environment: production
    WARNING: This is a development server. Do not use it in a production deployment.
    Use a production WSGI server instead.
   * Debug mode: on
    Traceback (most recent call last):
   File "/home/najibfahs/testing/app.py", line 414, in <module>
    app.run(debug=True)
   File "/usr/lib/python3.8/site-packages/flask/app.py", line 990, in run
    run_simple(host, port, self, **options)
   File "/usr/lib/python3.8/site-packages/werkzeug/serving.py", line 988, in run_simple
    s.bind(server_address)
  OSError: [Errno 98] Address already in use
 >>> extractImages(CAROUSEL_FOLDER)
 ['/static/images/carousel/slide-2.jpg', '/static/images/carousel/slide-4.jpg', '/static/images/carousel/slide-1.jpg', '/static/images/carousel/slide-3.jpg']
 >>> extractImages(GALLERY_FOLDER)
   ['/static/images/gallery/5.jfif', '/static/images/gallery/14.jfif', '/static/images/gallery/7.jfif', 
   '/static/images/gallery/3.jfif', '/static/images/gallery/2.jfif', '/static/ima
  ges/gallery/1.jfif', '/static/images/gallery/4.jfif', '/static/images/gallery/13.jfif', '/static/images/gallery/10.jfif', 
 '/static/images/gallery/6.jfif', '/static/images/gallery/
 16.jfif', '/static/images/gallery/8.jfif', '/static/images/gallery/15.jfif', '/static/images/gallery/12.jfif', 
 '/static/images/gallery/9.jfif', '/static/images/gallery/11.jfif']
 >>>

So i do get the desired output from the function when executed on pythonanywhere. but for some reason, i dont get the output from the home endpoint.

When you run it in the console, you have cd'd into a specific directory. That is what sets the working directory. If you are using relative paths and you have not set the working directory in your web app to the correct directory, then you will have this problem.

@Glenn After my 2nd post (response to the first reply), I did visit the link you provided (http://help.pythonanywhere.com/pages/NoSuchFileOrDirectory/). You were right! That fixed my issue. instead of checking the relative path of the folder passed to the extractImages(folder) function, I modified to become an absolute path:

abspath = os.path.join(os.path.dirname(os.path.abspath(__file__)), folder)

So the extractImages function became:

def extractImages(folder):
# variable to store links to the images in the folder
images = []
#use below abspath for localhost
#abspath = folder

#use below abspath for deployed version on python anywhere
abspath = os.path.join(os.path.dirname(os.path.abspath(__file__)), folder)
print(abspath)

if os.path.exists(abspath):
    entries = os.scandir(abspath)
    for entry in entries:
        #print(entry)
        if entry.is_file():
            filename = entry.name
            if allowed_file(filename):
                filepath = os.path.normpath(os.path.join(abspath, filename))
                print(filepath)
                images.append(filepath)
return images

This also explains why my upload forms are failing! thanks a lot!

Excellent, glad we could help!

There is a small error in the code.<br /> This line:

filepath = os.path.normpath(os.path.join(abspath, filename))

should be:

filepath = os.path.normpath(os.path.join(folder, filename))

Hi @ najibfahs, I had a similar problem

def extractImages(folder):
# variable to store links to the images in the folder
images = []
if os.path.exists(folder):
    entries = os.scandir(folder)
    for entry in entries:
        #print(entry)
        if entry.is_file():
            filename = entry.name
            if allowed_file(filename):
                filepath = os.path.join("/" + folder, filename)
                images.append(os.path.normpath(filepath))
return images

Looks like the os path join solution by giles works well, thanks a lot!

Glad to hear that it works for you!

Hi, I have a question about displaying an output on my webpage.

I run a certain function, calculate the output and assign it to a variable called result:

result = daily_run(x, y)

When I just implemented it, I had no issue displaying it with the below line of code in the HTML return section:

return '''
    <html>
        <body>
            <p>{result} </p>
        </body>
    </html>
'''

The output is a pandas df and it was showing a table before. However, something happened and now the output is literally the word " {result} ".

Could you please help me fix this? Thanks!

It looks like you're trying to use a Python f-string, but missed out the "f" in front of the three quotes at the start of the string. To have the effect you want it should be this:

return f'''
    <html>
        <body>
            <p>{result} </p>
        </body>
    </html>
'''

I have a challenge. I have setup my django application, then passes a message "Hello world" successfully deployed to the browser. But when I changed the view file and loaded the actual view function to deploy the app, I keep getting the "Server Error (500)". Can someone help me, please.

I'd suggest starting the debugging with checking your web app's error log (the most recent messages are at the bottom).

MY RECENT SERVER ERROR LOGS

2023-09-23 07:28:02 Sat Sep 23 07:28:02 2023 - received message 0 from emperor
2023-09-23 07:28:02 SIGINT/SIGTERM received...killing workers...
2023-09-23 07:28:03 worker 1 buried after 1 seconds
2023-09-23 07:28:03 goodbye to uWSGI.
2023-09-23 07:28:03 VACUUM: unix socket /var/sockets/abuokwen.pythonanywhere.com/socket removed.
2023-09-23 07:28:09 *** Starting uWSGI 2.0.20 (64bit) on [Sat Sep 23 07:28:07 2023] ***
2023-09-23 07:28:09 compiled with version: 9.4.0 on 22 July 2022 18:35:26
2023-09-23 07:28:09 os: Linux-5.15.0-1021-aws #25~20.04.1-Ubuntu SMP Thu Sep 22 13:59:08 UTC 2022
2023-09-23 07:28:09 nodename: green-liveweb38
2023-09-23 07:28:09 machine: x86_64
2023-09-23 07:28:09 clock source: unix
2023-09-23 07:28:09 pcre jit disabled
2023-09-23 07:28:09 detected number of CPU cores: 4
2023-09-23 07:28:09 current working directory: /home/abuokwen
2023-09-23 07:28:09 detected binary path: /usr/local/bin/uwsgi
2023-09-23 07:28:09 *** dumping internal routing table ***
2023-09-23 07:28:09 [rule: 0] subject: path_info regexp: \.svgz$ action: addheader:Content-Encoding:gzip
2023-09-23 07:28:09 *** end of the internal routing table ***
2023-09-23 07:28:09 chdir() to /home/abuokwen/
2023-09-23 07:28:09 your processes number limit is 128
2023-09-23 07:28:09 your memory page size is 4096 bytes
2023-09-23 07:28:09 detected max file descriptor number: 123456
2023-09-23 07:28:09 building mime-types dictionary from file /etc/mime.types...
2023-09-23 07:28:09 567 entry found
2023-09-23 07:28:09 lock engine: pthread robust mutexes
2023-09-23 07:28:09 thunder lock: disabled (you can enable it with --thunder-lock)
2023-09-23 07:28:09 uwsgi socket 0 bound to UNIX address /var/sockets/abuokwen.pythonanywhere.com/socket fd 3
2023-09-23 07:28:09 Python version: 3.10.5 (main, Jul 22 2022, 17:09:35) [GCC 9.4.0]
2023-09-23 07:28:09 PEP 405 virtualenv detected: /home/abuokwen/.virtualenvs/venv
2023-09-23 07:28:09 Set PythonHome to /home/abuokwen/.virtualenvs/venv
2023-09-23 07:28:09 *** Python threads support is disabled. You can enable it with --enable-threads ***
2023-09-23 07:28:09 Python main interpreter initialized at 0x5571f3827e70
2023-09-23 07:28:09 your server socket listen backlog is limited to 100 connections
2023-09-23 07:28:09 your mercy for graceful operations on workers is 60 seconds
2023-09-23 07:28:09 setting request body buffering size to 65536 bytes
2023-09-23 07:28:09 mapped 334256 bytes (326 KB) for 1 cores
2023-09-23 07:28:09 *** Operational MODE: single process ***
2023-09-23 07:28:09 initialized 38 metrics
2023-09-23 07:28:09 WSGI app 0 (mountpoint='') ready in 2 seconds on interpreter 0x5571f3827e70 pid: 1 (default app)
2023-09-23 07:28:09 *** uWSGI is running in multiple interpreter mode ***
2023-09-23 07:28:09 gracefully (RE)spawned uWSGI master process (pid: 1)
2023-09-23 07:28:09 spawned uWSGI worker 1 (pid: 2, cores: 1)
2023-09-23 07:28:09 metrics collector thread started
2023-09-23 07:28:09 spawned 2 offload threads for uWSGI worker 1
2023-09-23 07:28:13 announcing my loyalty to the Emperor...

This is the server.log, but the errors you're looking for will be in the error.log

THIS IS MY ERROR LOG

2023-09-23 09:49:05,291: Internal Server Error: / Traceback (most recent call last):   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/home/abuokwen/educrs/studentmgt/views.py", line 14, in index
    assert isinstance(request, HttpRequest) NameError: name 'HttpRequest' is not defined 2023-09-23 09:49:44,401: Internal Server Error: / Traceback (most recent call last):   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/home/abuokwen/educrs/studentmgt/views.py", line 14, in index
    assert isinstance(request, HttpRequest) NameError: name 'HttpRequest' is not defined 2023-09-23 09:50:05,059: Internal Server Error: / Traceback (most recent call last):   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/home/abuokwen/educrs/studentmgt/views.py", line 14, in index
    assert isinstance(request, HttpRequest) NameError: name 'HttpRequest' is not defined 2023-09-23 09:52:10,209: Internal Server Error: / Traceback (most recent call last):   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/home/abuokwen/educrs/studentmgt/views.py", line 14, in index
    assert isinstance(request, HttpResponse) AssertionError 2023-09-23 10:13:45,677: Internal Server Error: / Traceback (most recent call last):   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/home/abuokwen/educrs/studentmgt/views.py", line 14, in index
    assert isinstance(request, HttpResponse) AssertionError 2023-09-23 10:14:49,413: Internal Server Error: / Traceback (most recent call last):   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)   File "/home/abuokwen/.virtualenvs/venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/home/abuokwen/educrs/studentmgt/views.py", line 14, in index
    assert isinstance(request, HttpResponse) AssertionError

"NameError: name 'HttpRequest' is not defined" looks like the missing import.