pip3 install works, but then running the python script the module is not found - python-3.x

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?

Related

I get "syntax error" while installing numpy [duplicate]

I'm trying to use pip to install a package. I try to run pip install from the Python shell, but I get a SyntaxError. Why do I get this error? How do I use pip to install the package?
>>> pip install selenium
^
SyntaxError: invalid syntax
pip is run from the command line, not the Python interpreter. It is a program that installs modules, so you can use them from Python. Once you have installed the module, then you can open the Python shell and do import selenium.
The Python shell is not a command line, it is an interactive interpreter. You type Python code into it, not commands.
Use the command line, not the Python shell (DOS, PowerShell in Windows).
C:\Program Files\Python2.7\Scripts> pip install XYZ
If you installed Python into your PATH using the latest installers, you don't need to be in that folder to run pip
Terminal in Mac or Linux
$ pip install XYZ
As #sinoroc suggested correct way of installing a package via pip is using separate process since pip may cause closing a thread or may require a restart of interpreter to load new installed package so this is the right way of using the API: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'SomeProject']) but since Python allows to access internal API and you know what you're using the API for you may want to use internal API anyway eg. if you're building own GUI package manager with alternative resourcess like https://www.lfd.uci.edu/~gohlke/pythonlibs/
Following soulution is OUT OF DATE, instead of downvoting suggest updates. see https://github.com/pypa/pip/issues/7498 for reference.
UPDATE: Since pip version 10.x there is no more get_installed_distributions() or main method under import pip instead use import pip._internal as pip.
UPDATE ca. v.18 get_installed_distributions() has been removed. Instead you may use generator freeze like this:
from pip._internal.operations.freeze import freeze
print([package for package in freeze()])
# eg output ['pip==19.0.3']
If you want to use pip inside the Python interpreter, try this:
import pip
package_names=['selenium', 'requests'] #packages to install
pip.main(['install'] + package_names + ['--upgrade'])
# --upgrade to install or update existing packages
If you need to update every installed package, use following:
import pip
for i in pip.get_installed_distributions():
pip.main(['install', i.key, '--upgrade'])
If you want to stop installing other packages if any installation fails, use it in one single pip.main([]) call:
import pip
package_names = [i.key for i in pip.get_installed_distributions()]
pip.main(['install'] + package_names + ['--upgrade'])
Note: When you install from list in file with -r / --requirement parameter you do NOT need open() function.
pip.main(['install', '-r', 'filename'])
Warning: Some parameters as simple --help may cause python interpreter to stop.
Curiosity: By using pip.exe you actually use python interpreter and pip module anyway. If you unpack pip.exe or pip3.exe regardless it's python 2.x or 3.x, inside is the SAME single file __main__.py:
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
To run pip in Python 3.x, just follow the instructions on Python's page: Installing Python Modules.
python -m pip install SomePackage
Note that this is run from the command line and not the python shell (the reason for syntax error in the original question).
I installed python and when I run pip command it used to throw me an error like shown in pic below.
Make Sure pip path is added in environmental variables. For me, the python and pip installation path is::
Python: C:\Users\fhhz\AppData\Local\Programs\Python\Python38\
pip: C:\Users\fhhz\AppData\Local\Programs\Python\Python38\Scripts
Both these paths were added to path in environmental variables.
Now Open a new cmd window and type pip, you should be seeing a screen as below.
Now type pip install <<package-name>>. Here I'm installing package spyder so my command line statement will be as pip install spyder and here goes my running screen..
and I hope we are done with this!!
you need to type it in cmd not in the IDLE. becuse IDLE is not an command prompt if you want to install something from IDLE type this
>>>from pip.__main__ import _main as main
>>>main(#args splitted by space in list example:['install', 'requests'])
this is calling pip like pip <commands> in terminal. The commands will be seperated by spaces that you are doing there to.
If you are doing it from command line,
try -
python -m pip install selenium
or (for Python3 and above)
python3 -m pip install selenium

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?

conda installed some package but still ModuleNotFoundError when import this package

I've found the solution:anaconda - graphviz - can't import after installation
I want to use graphviz and follow the commend in https://anaconda.org/anaconda/graphviz
run following in terminal
conda install -c anaconda graphviz
However no matter in Jupyter Notebook, python or Pycharm to import graphviz, it always shows
ModuleNotFoundError: No module named 'graphviz'
How to solve this problem? Thank you.
PS:
when run which python in terminal: it return /opt/anaconda3/bin/python, therefore I use anaconda environment by default. And I have only one environment in anaconda that is root.
when I run conda list in terminal, I can find this line :
graphviz 2.40.1 hefbbd9a_2
I found a weird thing:
my pip and conda use the same environment:
run :which pip
get : /opt/anaconda3/bin/pip
run : which conda
get : /opt/anaconda3/bin/conda
However when I run pip list, I cannot find graphviz and many other packages which shows in conda list. For these packages show in conda list but not in pip list, I also cannot import them no matter in Jupyter notebook, python, pycharm etc. Why this happens?
After using "conda install attrs", other package installations are working fine without any http connection or ModuleNotFoundError errors. Please try and let me know.

"No module named... "when running python from terminal

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.

Installing module beautifulsoup4

my problem is with installing module "beautifulsoup4". In pycharm i installed the package correctly but when im trying to import the module it says no module found with that name.and i searched the internet for solution and i found a way for that .
It says in terminal use this command :
"pip install beautifulsoup4 "
But still pycharm or id le shell cant find the module.even i tried the command :
"pip install beautifulsoup4 --upgrade"
and terminal says its already up-to date.
I think the problem is with your version of python selected in pycharm and the version of python you installed BeautifulSoup
If using python3
pip3 install BeautifulSoup
or
pip3 install bs4
in case of python2 replace pip3 with pip
try to change python interpreter in pycharm settings.
alternatively try ( only for Ubuntu and Like Linux)
I can't remember the exact package.
try
apt-get install python3-bs4
or
apt-get python3-BeautifulSoup
again do it with python if you want for python2
use pip3:
pip3 install beautifulsoup4

Resources