"No module named... "when running python from terminal - python-3.x

I've Python 3.6 Installed on my ubuntu. When i try to run my codes via terminal, python doesnt recognize the modules I've installed with pip3. Lets take flask as an example. In the terminal I can do:
python3
import flask
And I dont get any Import error.But when I've a python file in any location which contains:
import flask
And i run it via terminal:
sudo python file_name.py
I get the following error:
ImportError: No module named flask
Why python doesnt recognize the modules?

check if flask is installed properly:
try pip3 freeze or pip3 list from the location where you are trying to run and check if it has flask is in the list.
if it doesn't exists then reinstall and try.

Related

vs code can not import python package but terminal can import

I installed anaconda for python3.
I clone a package from GitHub(can not install by pip3) and set the path to python in ~/.bash_profile.
In the terminal, I can import the package successfully. when I run scripts with vs code, error info occurred: ModuleNotFoundError: No module named mypackage
I run which python3 from the terminal and got: /usr/bin/python3
I select the same python interpreter(command+shift+p) in vscode which point to the same path : /usr/bin/python3
I try to search from StackOverflow, most of the answers said terminal and vs code use the different python version, but it seems my python version is the same.
I don't have a clue and am stuck in here for a whole day. Can someone help me?

Import Error Can't import Requests on Idle, but can on Jupyter/spider

For whatever reason when I try to import requests on idle/shell I get this error message : ModuleNotFoundError: No module named 'requests'.
I have installed it with pip and I still get an error.
It works 100 percent of the time on Jupyter notebook for me.
You have to install the requests module. pip is the easiest option, but that is not a Python command. First Find the directory where Python installed. Make sure you open CMD "Run as Admin", and run the following commands: for example
C:\Python27\Scripts\pip install requests

Attach psycopg2 to python3.7

My RHEL7 has Python 2.7. I installed python3.7 and also installed psycopg2. The issue is when I run the python3 script where psycopg2 module is used, I am getting error below:
ModuleNotFoundError: No module named 'psycopg2'
I cant figure out how to install/attach psycopg2 for python 3.
I tried reintalling pyscopg2. No luck!

Imported package not available in Jupyter-Python

Importing pysftp into Jupyter Notebook
While importing pysftp into Jupyter Notebook, ModuleNotFoundError is shown.
Checking import of pysftp on device?
I have verified the package installation with
pip list and pip show pysftp
Had imported pysftp package(v0.2.9) and installed it in the below location
C:\users\xxxxxx\appdata\roaming\python\python37\site-packages
Check : Package installed OKAY
Check about package correct path linking from cmd prompt?
I'm using Python 3.7.0 on a WIN machine, verifyed the site package location using
import sys and sys.path
image confirms linking of PATH to correct location and the package is successfully executed when python is run through cmd prompt
Check : Path link and cmd run OKAY
Now could anyone help me solve why the package import in Jupyter Notebook is throwing an error?
Thank you
Edit 1: Check for different environment installed? added based on one of the answer
Only one environment is present in the machine
I got the same. I solved by installing directly within Jupyter using the following command:
import sys
!{sys.executable} -m pip install dice-ml
Are you running the notebook through a virtual environment?
You can try running the same commands as you did on CMD by preceding it with ! as follows:
!pip list
Ideally this should list the same contents as shown in CMD. However the results may be different if you are running Jupyter notebook in a virtual environment. If you are unable to see pysftp, you need to install it within the virtual environment. This can be done from within your notebook as:
!pip install pysftp

pip3 install works, but then running the python script the module is not found

I'm currently doing
pip3 install reportlab
and that works fine. However in my script I have a
from reportlab.pdfgen import canvas
Running my script results in a module reportlab not found. To run the script I am using python3 name_of_script.py, but python name_of_script.py also doesn't work. Any ideas?

Resources