#NextJS Flask Import Error

35 messages · Page 1 of 1 (latest)

hot nest
#

I have a Nextjs app that is running a flask backend using their starter template. Everything was going great until I started to import new packages. For some reason, when I run npm run dev (which calls the following script pip3 install -r requirements.txt && python3 -m flask --app api/index run -p 5328 withe the following requirements numpy==1.24.3 Flask==2.2.2 Werkzeug==2.2.2 requests==2.31.0) it fails to recognize requests and outputs ModuleNotFoundError: No module named 'requests'. I have no idea why this is happening and am out of luck looking online.

low streamBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

hot nest
#

I did as much investigating as I could myself. Here was what I was able to gather. Even when I have the smallest flask application possible, if I am importing anything other than from flask import Flask I get the ModuleNotFoundError even though I can clearly see that it was installed as I had the npm run dev script run pip install.

hot nest
#

Okay so new update, if I run flask run within the folder api it works. However, npm run dev no longer works, this is the main issue as I won't be able to push any new changes to prod. Ultimately the issue is why does flask run work and python3 -m flask --app api/index run not?

cloud crow
#

does npm run dev run nextjs or vercel?

#

as nextjs cli just does nodejs

#

but vercel cli can do python

hot nest
#

This is my package.json
{ "name": "next-flask", "version": "0.1.0", "private": true, "scripts": { "flask-dev": "pip3 install -r requirements.txt && python3 -m flask --app api/index run -p 5328", "next-dev": "next dev", "dev": "concurrently \"pnpm run next-dev\" \"pnpm run flask-dev\"", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@emotion/react": "^11.11.1", "@emotion/styled": "^11.11.0", "@mui/material": "^5.14.14", "@supabase/auth-helpers-nextjs": "^0.8.1", "@supabase/supabase-js": "^2.38.2", "@types/node": "20.1.4", "@types/react": "18.2.6", "@types/react-dom": "18.2.4", "autoprefixer": "10.4.14", "concurrently": "^8.0.1", "eslint": "8.40.0", "eslint-config-next": "13.4.2", "mic-recorder-to-mp3": "^2.2.2", "next": "13.4.3", "postcss": "8.4.23", "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.3.2", "typescript": "5.0.4" } }

cloud crow
#

ohh, you are running them individually

hot nest
#

It runs flask dev then it runs the normal npm start

cloud crow
#

but on diferent ports im guessing

hot nest
#

yes on different ports

#

flask is on 5328 while the frontend is on 3000

cloud crow
#

and which part is outputting the error? (nextjs or your flask run)

hot nest
#

the flask run

cloud crow
#

hmm i don't know much python

#

so im sorry that i can't help much there 😭

hot nest
#

This is what I get when I run the aforementioned command:
`
(venv) PS D:\github\audio-notes\api> python3 -m flask --app api/index run -p 5328
Usage: python -m flask run [OPTIONS]
Try 'python -m flask run --help' for help.

Error: While importing 'index', an ImportError was raised:

Traceback (most recent call last):
File "C:\Users\Andres\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\flask\cli.py", line 219, in locate_app
import(module_name)
File "D:\github\audio-notes\api\index.py", line 2, in <module>
import openai
ModuleNotFoundError: No module named 'openai'
`

#

No worries

past stirrup
#

Did you add openai to your requirements.txt and was it installed using pip in your venv?
Do you run npm run dev from within the same venv as the standalone version?

hot nest
#

I added open ai to the requirements.txt and it was installed using pip in my env

#

I also ran npm run dev within the same venv

#

Requirements.txt
numpy==1.24.3 Flask==2.2.2 Werkzeug==2.2.2 openai==0.28.1 requests==2.31.0 Flask-Cors==3.0.10

#

The reason why npm run dev isn't working is because the command it runs (which was given as part of the nextjs flask starter template) python3 -m flask --app api/index run -p 5328 no longer works

past stirrup
#

Just a shot into the blue: Try starting the python interpreter with a fully qualified path. Should be D:\github\audio-notes\venv\Scripts\python in your case. I wonder if something is wrong with your path settings, since the flask library used seems to be a global one and you said you installed everything in your venv.

hot nest
#

That makes so much sense!

#

I can try to set this up

hot nest
#

How would I go about starting my python interpreter with a fully qualified path?

past stirrup
#

Try like so at the promt: D:\github\audio-notes\venv\Scripts\python -m flask --app api/index run -p 5328

hot nest
#

That worked you are a genius

#

but how do I fix it so that my npm run dev works

#

I assume that I need to fix my virtual environment somehow?

#

how do you recommend I set it up?

past stirrup
#

When you activate the virtual environment your PATH environment variable should be prepended by the Scripts directory inside the venv. Somehow that doesn't seem to be the case in your setup. Try to reactivate your virtual environment, then check which directories PATH points to. Also check that python3 exists in the Scripts directory of your venv.