Installing pypiwin32 successful; cannot import module - python-3.x

Title says it all;
Tried typical cmd commands like:
pip install pypiwin32
python -m pip install pypiwin32
Python script:
import pip
def install(package):
pip.main(['install', package])
if __name__ == '__main__':
install("pypiwin32") # also tried 'pywin32'
I run the above script and get the message:
Requirement already satisfied: pypiwin32 in 'path'
Then run test command:
import pypiwin32
and get:
builtins.ModuleNotFoundError: No module named 'pypiwin32'
Is there something wrong with my path? Do i HAVE to install it as a .whl file?
Not sure what I am doing wrong here. I am wanting a module to convert text to speech.

Ok, not sure if i should answer my own question or delete this, but it may help someone in the future.
I went into my python path and checked my .pyd files, it turns out there was no module called pypiwin32. instead the module is called win32com.pyd
So using the following code:
import win32com.client as wincl
wincl.Dispatch("SAPI.SpVoice").Speak("Speak English, parle français")

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

Module not found when I tried to import pyPDF2

My python version is 3.6. I am able to install the pyPDF2.
Ran pip install pyPDF2 successfully.
Ran pip list, it shows up as 1.26.0
My environment is not base, but I set up an environment as pytorch. pyPDF2 is installed successfully in this environment.
It pops error when I tried to import it. (typo fixed from the original post)
Your import pyPDF is incorrect. Instead try import PyPDF2.
Is also possible your pip does not match your python (this can be the case when multiple python versions are installed at the same time). Try running python -m pip list to confirm what exactly is installed.

python install wheel leads to import error

I'd like to make a wheel binary distribution, intstall it and then import it in python. My steps are
I first create the wheel: python ./my_package/setup.py bdist_wheel
I install the wheel: pip install ./dist/*.whl
I try to import the package: python -c"import my_package"
This leads to the error:
ImportError: No module named 'my_package'
Also, when I do pip list, the my_package is listed.
However, when I run which my_packge, nothing is shown.
When I run pip install ./my_package/ everything works as expected.
How would I correctly build and install a wheel?
python version 3.5
pip version 10.1
wheel version 0.31.1
UPDATE:
When I look at the files inside my_package-1.0.0.dist-info, there is an unexpected entry in top_level.txt. It is the name of the folder where I ran
python ./my_package/setup.py bdist_wheel in. I believe my setup.py is broken.
UPDATE WITH REGARDS TO ACCEPTED ANSWER:
I accepted the answer below. Yet, I think it is better to simply cd into the package directory. Changing to a different directory as suggested below leads to unexpected behavior when using the -d flag, i.e. the target directory where to save the wheel. This would be relative to the directory specified in the setup.py file.
I had the very same error, but it was due to my setup.py not specifying the entry "packages=setuptools.find_packages()".
Everythings builds nicely without that but you can't import anything even though pip shows it to be installed.
If you need to execute the setup script from another directory, ensure you are entering the project dir in the script.
from setuptools import setup
root = os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir))
os.chdir(root)
# or using pathlib (Python>=3.4):
import patlib
root = pathlib.Path(__file__).parent
os.chdir(str(root))
setup(...)
In my case, in order to solve it I just had to upgrade pip (since Docker installed pip 9).
python3 -m pip install --upgrade pip
I have experienced the same situation, maybe not for the same reason, here just for reference.
The package name should not contain the dash "-", there's no error pop out, but after installing your wheel, though it is shown in pip list, you can't find that package.
/src/your-package-name # should not
/src/your_package_name # should like this
In the setup.py, you can use the name with dash "-" without limitation:
setuptools.setup(
name="instrument-lab",
...

Python ModuleNotFoundError: No module named 'win32event'

in my Python Script there is an ERROR with the line
import win32events
I still installed the package pypiwin32 (with pip install pypiwin32) but it seems that this is nor the right package.
Does someone know what I need to install to use win32events in Python?
You should install pywin32 package (not pypiwin32)!!!

Pip packages not importing

So I am unable to use the package PyMySQL from pip.
I've tried installing it with pip, pip3, and easy install.
I've tried running the file in python2 and python3.
I've tried using
python -m pip install ...
I'm on mac.
My ~/.bash_profile has the following
export PATH="/usr/local/sbin:$PATH"
export PATH="/Users/taylorcochran/anaconda2/bin:$PATH"
export PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH="/usr/local/cellar/openvpn:$Path"
export PATH="/Users/taylorcochran/anaconda3/bin:$PATH"
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin"
export PYTHONPATH="/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages:$PATH"
I've looked at similar problem across StackOverflow but none of the suggested solutions produce any results.
Additionally, when I try to add # PATH="/usr/local/lib"
to my path it breaks the built in commands like ls and cd
Edit:
I'm running the script using python3 Test.py which simply contains the line import PyMySQL
Which outputs: ModuleNotFoundError: No module named 'PyMySQL'
Tried:
pip3 install --install-option="--prefix=/Library/Frameworks/Python.framewor k/Versions/3.6" PyMySQL
Outputs:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip/commands/install.py:194: UserWarning: Disabling all use of wheels due to the use of --build-options / --global-options / --install-options. cmdoptions.check_install_build_global(options) Requirement already satisfied: PyMySQL in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages
And still doesn't let my import the module
You have Python 3.6 in the PYTHONPATH so you should go with pip3. Try to use the following command:
pip install --install-option="--prefix=$PREFIX_PATH" package_name
where $PREFIX_PATH in your case should be /Library/Frameworks/Python.framework/Versions/3.6
When you use Python located in /Library/Frameworks/Python.framework/Versions/3.6/bin the module library should be available.

Resources