'from main import db' gives ModuleNotFoundError: No module named 'main' - python-3.x

I created an SQLite3 database, called page.db.
I am trying to use it in python.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import sqlite3
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']="sqlite:////mnt/home/cc/page.db'
db=SQLAlchemy(app)
class tabb(db.Model):
name=db.Column(db.Username,primary_key=True)
After writing this code, I enter python shell and run the command from main import db. It shows an error saying:
ModuleNotFoundError: No module named 'main'
How can I fix this?

Are you sure the filname is main and that you are in the same directory as main.py when opening the python interpreter?

open terminal and then cd (projectname), after try again

Related

ModuleNotFoundError: No module named 'module' while importing my own module VsCode MacOs Python 3.10

Structure:
|--folder/
|--a.py
|--main.py
When loading module 'main' into module 'a'
#a.py
import main
the following error occurs - ModuleNotFoundError: No module named 'main'.
PyCharm copes with this task, but VSCode does not. What's the matter?
Looking at your directory structure, I presume that a.py and main.py are in different directories. If that is the case, then the answers to [this question][1] should be useful. To elaborate a bit further, you can use the sys module to specify the path to the other module (main.py) i.e. if main.py is in a different directory from a.py.
import sys
sys.path.append('/path_to_main_module_directory/')
import main
print("This import is for the main module")
I hope this helps!
[1]: Importing files from different folder

ModuleNotFoundError: No module named 'flask_wtf' ... verified installed in virtualenv

My import statements are as follows:
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired
I am able to execute the file - blank.py - within Pycharm and do not receive a ModuleNotFoundError.
Whenever I execute flask run in terminal I get ModuleNotFoundError: 'flask_wtf' and it traces back to 'blank.py'.
I am not sure why this is happening as I have both flask and flask-wtf installed globally and locally. I have never had this problem when working with other libraries.
How do I fix this? Thank you for you help.

Unable to run python script, error shows ModuleNotFoundError: No module named 'src'

Below is the python script(selenium webdriver) I want to execute. But I see error is being thrown that ModuleNotFoundError: No module named 'src'. I see the module src(folder) exist. I am trying to execute this in command prompt. Can some one please help me where I'm doing wrong.
from src.pages.base_page import BasePage
from src.pages.login_page import LoginPage
import unittest
class Dispatcher(BasePage, unittest.TestCase):
def setup(self):
super(Dispatcher,self).setup()
def login_eoc(self):
self.login_page.login()
test output:
C:\NASAuto\tests>py test_dispatcher.py
Traceback (most recent call last):
File "test_dispatcher.py", line 1, in
from src.pages.base_page import BasePage
ModuleNotFoundError: No module named 'src'
run a command line, go to the python folder,
python.exe import src
and verify if python is able to import src. If not try to reinstall this module
Else
You need to add that directory to the path:
import sys
sys.path.append('../src')
Maybe put this into a module if you are using it a lot.
good luck

"No module named" when importing from another package in python3.6

If I execute demo2.py it works fine, the problem is when I execute main.py
|myPackage
|subPackage
demo.py
demo2.py
main.py
main.py
from ludikDriver.demo2 import demo2_print
demo2_print()
demo2.py
from demo import demoprint
def demo2_print():
print("demo2")
demoprint()
demo2_print()
demo.py
def demoprint():
print("demo")
Error:No module named 'demo'
Just use relative imports as suggested in pep 328.
from .demo import demoprint
You can do for the other package. Just as relative paths.
Your modules need context of themselves. You should have the "__init__.py" file in subPackage and myPackage. Then your import should be relative:
from . import demo
OR more in context of your example:
from .demo import demoprint
Your error is associated with the top line in demo.py:
from demo import demoprint
There is no module named demo

Trying to import winsound

When I import winsound and then try to run the program, it returns an error message saying:
ImportError: No module named 'winsound'.
Are there any settings I need to change?
winsound only exists in Python installed under Windows. Do not attempt to import it if you are not running under Windows.

Resources