Cannot import module after installed by pip - python

I have already installed my wanted module from pip by using pip install but when i open up a program in idle and put import menu it says module not found what did i do wrong, im using python 3.7 and have the latest version of pip.

Python looks for its modules and packages in $PYTHONPATH. You'll need to figure out if the __init__.py module of the package you'd like to use is in python's path. To find out whether it is in python's path, run:
import sys
print(sys.path)
within python.
To add the path to your module, run:
sys.path.append("/path/to/your/package_or_module")
and you should be set.

I met the same problem, but in my case the package is created by myself. So this answer applyes to the custom packages. See Why customer python package can not be imported? for details.

Just in case someone is meeting a similar problem to mine.
Pay attention to the case of the package. In my case, the package is called 'shapely'. And I installed it through
pip install Shapely
But you have to import as
import shapely

Related

How to install troposphere for python3?

I'm running macOS version 11.0.1.
I'm trying to import a module called troposphere
I have Python2.7 as well as Python3.9.1 installed
When I try to import troposphere using Python2.7 like
#!/usr/bin/python
from troposphere import Template
It does work. But when I try to import the same using Python3.9.1 like
#!/usr/bin/python3
from troposphere import Template
It throws error like this
ModuleNotFoundError: No module named 'troposphere'
What should I do? Please help
it is because the python interpreter you use to run your code doesn't have troposphere installed.
Managing multiple Python version on the same computer is tricky, But I will try to explain it.
I assume that you are using python your_script.py to run your code, and pip install troposphere to install package right? But think of this, how does your system know exactly which python you are running and which python to install package? Here's how to check the full path of your python command and pip command.
type python command enter python console
then type this:
import sys
print(sys.executable)
3. navigate up and down little bit, you will find a bin folder include bin/python which is your interpreter and bin/pip which is your pip command. And there's a lib/site-packages folder, this is where third party library been installed to.
4. if you see import error, use the above method to locate the python interpreter and check the site-packages folder, most likely there's no such troposphere folder in it. Then you need to use the full path of the <path-to>/bin/pip to install troposphere. Then your problem is solved.
If you are using home brew or the python installer download from www.python.org, your system probablly already messed up. The recommended way to manage multiple Python version is to use pyenv https://github.com/pyenv/pyenv. It allows you to install any python version and easy to delete them.
Chek if the module was depreciated

What is importlib.util in python3?

I am trying to pip install importlib with python 3.6, but I get an Import Error saying: 'NO Module named "importlib.util"'. This also comes up when I try to pip install imagescanner, which is my real intention. Building an App that connects to Image Scanner Devices, but that's another problem...
Thanks for any help!
importlib is builtin with Python 3 (at least for me), you can import it directly without installing anything.
The error from pip install is possibly due to importlib is builtin and there's no distribution that's publicly available.
If your Python is from the last 5 years or so, then importlib is builtin. It is available as an external PyPI package for anyone using a much older Python that doesn't have it. You shouldn't be pip installing it, and modules that you are installing also shouldn't be pip installing it. That's a bug in the package you are trying to install, or one of the modules that it installs.

Django cant find pip3 installed module

I created a Python module and installed it using pip3. If i check on dist-package folder its there. If i import this module into a new Python project its ok.
Problem: I would like to use this module on my Django project, but when i try to import it can't be found.
Already tried: If i copy the module to site-package, it works but i don't get why i have to do this. I would like that this Python module installed with pip3 is visible for everyone without the need to copy/paste from dist folder.
Whatever the package you installed using pip3, those packages mapped to python3 version, and also will be installed in /usr/lib/python3.x/site-packages/
for your reference, What's the difference between dist-packages and site-packages?

ImportError: no module named win32api

I am using Python 2.7 and I want to use pywin32-214 on Windows 7. I installed pywin32-214 by using the MSI installer. But when I import win32api in my Python script, it throws the error:
no module named win32api
What should I do? How can I use win32api on Windows 7?
This is resolve my case as found on
Where to find the win32api module for Python?
pip install pypiwin32
According to pywin32 github you must run
pip install pywin32
and after that, you must run
python Scripts/pywin32_postinstall.py -install
I know I'm reviving an old thread, but I just had this problem and this was the only way to solve it.
I had an identical problem, which I solved by restarting my Python editor and shell. I had installed pywin32 but the new modules were not picked up until the restarts.
If you've already done that, do a search in your Python installation for win32api and you should find win32api.pyd under ${PYTHON_HOME}\Lib\site-packages\win32.
I didn't find the package of the most voted answer in my Python 3 dist.
I had the same problem and solved it installing the module pywin32:
In a normal python:
pip install pywin32
In anaconda:
conda install pywin32
My python installation (Intel® Distribution for Python) had some kind of dependency problem and was giving this error. After installing this module it stopped appearing.
I had both pywin32 and pipywin32 installed like suggested in previous answer, but I still did not have a folder ${PYTHON_HOME}\Lib\site-packages\win32.
This always lead to errors when trying import win32api.
The simple solution was to uninstall both packages and reinstall pywin32:
pip uninstall pipywin32
pip uninstall pywin32
pip install pywin32
Then restart Python (and Jupyter).
Now, the win32 folder is there and the import works fine. Problem solved.
After installing pywin32
Steps to correctly install your module (pywin32)
First search where is your python pip is present
1a. For Example in my case location of pip -
C:\Users\username\AppData\Local\Programs\Python\Python36-32\Scripts
Then open your command prompt and change directory to your pip folder location.
cd C:\Users\username\AppData\Local\Programs\Python\Python36-32\Scripts
C:\Users\username\AppData\Local\Programs\Python\Python36-32\Scripts>pip install
pypiwin32
Restart your IDE
All done now you can use the module .
The following should work:
pip install pywin32
But it didn't for me. I fixed this by downloading and installing the exe from here:
https://github.com/mhammond/pywin32/releases
This line:
import win32com
got me the error no module named win32api.
Using this command in elevated terminal:
pip install pywin32-ctypes and
pip install pywin32
and based on the error displayed, replacing:
import win32api → from win32ctypes.pywin32 import win32api
import pywintypes → from win32.lib import pywintypes
import _win32sysloader → from win32 import _win32sysloader
in your source file, or even the files of the packages that report the error (know what you are doing if you choose this approach) may solve this error. But better would be to just add the corresponding directories into the python path variable, for better integration with the python loading system, more info here: https://realpython.com/python-import/
So I put this content:
python38.zip
.
./lib
./lib/site-packages
./lib/site-packages/win32
./lib/site-packages/win32/lib
./lib/site-packages/win32ctypes/pywin32
./lib/site-packages/win32ctypes
# Uncomment to run site.main() automatically
#import site
(order DOES matter)
into this file: <python_root_installation_directory>/python38._pth
That way, correct libraries load when standard imports are used. If there is a cache import somewhere in the library, it will work, and the imports inside the libraries work as well.
This works for me and my installation, so your environment may be set up differently and this guide may not be fully compatible, but it is a good step in solving the issue, maybe modification or extension of my steps above may lead to the solution in another distribution.
Try this, it worked for me, it may help you!
pip install pywin32==225
I've tried all of your answers and finally got a solution. my issue was that I installed from both pip and python interpreter on my Pycharm IDE. I just removed win32compact from my interpreter and it works.
let me summarize, correct me if wrong, as below:
# update to newest pywin32
python -m pip install -U pywin32 pypiwin32
# run the post-install #ref https://stackoverflow.com/questions/21343774/importerror-no-module-named-win32api
python %CONDA_PREFIX%\Scripts\pywin32_postinstall.py -install
# double check
python -c "print( __import__('win32api') )"
In my case, the only thing that worked was to download the appropriate wheel from: https://pypi.org/project/pywin32/#files, and install with --force-reinstall.
pip install pywin32-300-cp37-cp37m-win_amd64.whl --force-reinstall
I found solution here:
https://www.ti-enxame.com/pt/python/pywin32-e-python-3.8.0/813327700/
I was able to run it on Spyder without error, but It wasn't working on cmd prompt
I just import the module pywintypes before win32api
import pywintypes
import win32api
I tried to reinstall pywin32, installed different versions, but nothing could make pywin work. The only thing that did finally help me was running
python pywin32_postinstall.py
which is located at Anaconda3\Scripts folder. Thanks for sameer_nubia for highlighting the location.
I solve this by
python -m pip install -U pywin32 pypiwin32

Python: I can't import a module even though it's in site-packages

I'm following the tutorial "Think Python" and I'm supposed to install the package called swampy. I'm running python 2.7.3 although I also have python 3 installed. I extracted the package and placed it in site-packages:
C:\Python27\Lib\site-packages\swampy-2.1.1
C:\Python31\Lib\site-packages\swampy-2.1.1
But when i try to import a module from it within python:
import swampy.TurtleWorld
I just get no module named swampy.TurtleWorld.
I'd really appreciate it if someone could help me out, here's a link to the lesson if that helps:
http://www.greenteapress.com/thinkpython/html/thinkpython005.html
If anyone else is having trouble with this on Windows, I just added my sites-package directory to my PATH variable and it worked like any normal module import.
C:\Python34\Lib\site-packages
Hope it helps.
I extracted the package and placed it in site-packages:
No, that's the wrong way of "installing" a package. Python packages come with a setup.py script that should be used to install them. Simply do:
python setup.py install
And the module will be installed correctly in the site-packages of the python interpreter you are using. If you want to install it for a specific python version use python2/python3 instead of python.

Categories

Resources