'Ubuntu Terminal Opens Python Instead of Running Python File

I'm trying to run a python script from the Ubuntu terminal with python3 index.py but Ubuntu just opens the Python terminal instead of running the actual script. This behaviour only started in the last few days and previous to that the code ran as expected.

This only happens in one specific folder and I believe it has something to do with the folder's virtual environment (created using venv). When the virtual environment isn't activated then python3 index.py tries to run the file, and I receive a ModuleNotFound error.

The folder that is causing the issue contains files that creates a dashboard web application, and they've been put in a Docker container, so I'm not sure if that has something to do with it.

Machine: Windows 11

Python: 3.8.10

Ubuntu: 20.04

Contents of problem folder:

|apps
  -competitors.py
  -data.py
  -functions.py
  -media_posts.py
  -overview.py
  -post_performance.py
  -text.py
|assets
|env
-.env
-app.py
-Dockerfile
-index.py
-requirements.txt

Contents of the index.py file:

import dash_html_components as html
import dash_core_components as dcc
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
from app import app, server
from apps import overview, media_posts, competitors, post_performance, overview, text


header = dbc.Container([
    dbc.Row([
        dbc.Col(dcc.Link(html.Img(src="assets/nomadpursuit3.png"), href="https://www.instagram.com/nomadpursuit/"),
                className="col-4"),
        dbc.Col(html.H1("Instagram Performance"), className="col-6 col-center"),
        dbc.Col(html.P(), className="col-2")
    ], className="col-center")
])

# create the page layout using the previously created sidebar
app.layout = html.Div([
    dcc.Location(id="url", refresh=False),
    header,
    html.Div(id="page-content")
])


# callback to load the correct page content
@app.callback(Output("page-content", "children"),
              Input("url", "pathname"))
def create_page_content(pathname):
    if pathname == "/overview":
        return overview.layout
    elif pathname == "/media-posts":
        return media_posts.layout
    elif pathname == "/competitors":
        return competitors.layout
    elif pathname == "/post-performance":
        return post_performance.layout
    elif pathname == "/text-analysis":
        return text.layout
    else:
        return overview.layout


if __name__ == "__main__":
    print("Running in development mode")
    app.run_server(host="0.0.0.0", port=5000, debug=False)

Is anybody able to advise on what might be causing the issue please and a way to fix it without copying all of my files into a new folder location?



Solution 1:[1]

try with sudo

sudo python3 index.py

or sudo python index.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 brahim12