I've installed the module pyaudio using pip. However, when I try to import it, Python says the module is not found:
C:\Users\hp>pip install pyaudio
Requirement already satisfied: pyaudio in c:\users\hp\appdata\local\programs\python\python37\lib\site-packages (0.2.11)
>>> import pyaudio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pyaudio'
Why can't Python find the installed module?
(I am using Python 3.7.)
It happens quite often that someone installs a Python package using pip, but then can't seem to import it in Python. To understand why this happens, you must know how Windows finds executables to run, and how the Python software is installed. The basics:
When running a command, Windows searches for an executable in the environment variable PATH. It executes the first one found.
The Python interpreter, python.exe, is installed in <PYTHON_INSTALL_DIR> (e.g. C:\Python\3.7).
Python tools such as pip, pylint, virtualenv, PyCrust, etc., are installed in <PYTHON_INSTALL_DIR>\Scripts.
The Python launcher for Windows, py.exe, is installed in your Windows system directory (e.g. C:\Windows).
python and pip commands use the modules found in the directory their installed in, they do not look at PATH.
So, let's say you have the following Python versions:
C:\Python\2.7
C:\Python\3.6
C:\Python\3.7
and your PATH environment contains the following directories:
C:\Python\2.7
C:\Python\3.6\Scripts
then, see the following output:
C:\>python -V
Python 2.7.16
C:\>pip -V
pip 19.1.1 from c:\python\3.6\lib\site-packages\pip (python 3.6)
C:\>py -V
Python 3.7.3
So, when running pip, it is possible that the packages are installed in another Python version then the version you'll get when running python.
To see which versions are (correctly) installed on your system, run py -0p. Example output:
C:\>py -0p
Installed Pythons found by py Launcher for Windows
-3.7-64 C:\Python\3.7-64\python.exe *
-3.7-32 C:\Python\3.7-32\python.exe
-3.6-64 C:\Python\3.6-64\python.exe
-2.7-64 C:\Python\2.7-64\python.exe
-2.7-32 C:\Python\2.7-32\python.exe
General solution (for Windows)
The best thing is not to rely on your system PATH. Use the py launcher to select the version you want. To run the pip module corresponding to the Python version you want to use, start pip as a module instead of executable.
So instead of:
pip install <package>
run:
py -3.6 -m pip install <package>
To see which Python packages you have installed for that Python version, use:
py -3.6 -m pip freeze
Some additional remarks
Whether a Python installation is added to your PATH or not, is an option during the installation. If it is added, it is added at the beginning of the PATH, so the most recently installed Python version will be selected first.
The Windows system directory should always be in your PATH, so the py command will always be available, even if you did not add any Python installation to your PATH.
If you double-click on a .py file from Windows Explorer, or type the filename directly as a command in a Command Prompt (e.g. test.py), then the action is determined from the Windows registry. It is possible that the file will be opened in your IDE, or that it's executed using a Python interpreter. In that case, it is probably the most recently installed Python version. It's possible that the command python test.py, uses a different Python version than the command test.py.
Some installations also include executables named python2/python3 (not on Windows), pip3/pip3.7 (also on Windows), etc. This would also allow you to specify which version to use. These would be useful on systems where these binaries exist and are in the path.
You'd think python -m pip install pyaudio would solve the problem but it does not.
To install pyaudio you'd first need to install pipwin using:
pip install pipwin
pipwin is a complementary tool for pip on Windows. pipwin installs unofficial python packages.
Next, use pipwin to install pyaudio,
pipwin install pyaudio
Related
I can't install any python modules that require python 2.7 or later. I have uninstalled everything that Mac would let me that was related to python 2, and I run everything on python 3. I am completely lost. I am on Mac and whenever I try to install a module(like praw) this pops up.
I used the command
pip install praw
Collecting praw
Using cached https://files.pythonhosted.org/packages/41/89/94c1ec81a05536e2c2a1dc2e8f5402c8ad65963f28948bf41c64621e238b/praw-6.5.0-py2-none-any.whl
ERROR: Package 'praw' requires a different Python: 2.7.16 not in '>=3.5' ```
Welcome to Stack Overflow Malachi! I believe you're looking for the command pip3 (pip3 install praw) to install Python 3 packages through pip. If you open your terminal and type man pip to get the manual page, there is a line that says:
pip is the command to use when installing packages for Python 2, while pip3 is the command to use when installing packages for Python 3.
Looks like the most recent version of praw requires Python 3.5 or greater.
The last version that supported Python 2.7 was praw 5.4.0. If you're still using Python 2.7 and need this package, try running
pip install praw==5.4.0
If you're using Python 3.x, check to see if you're using the right version of pip. If you have both Python 2 and Python 3 installed on your system, you'll likely need to install system wide packages using pip3 instead if pip. In this case, try running
pip3 install praw
Alternatively, if you're targeting a specific Python interpreter (e.g. python3.7, python3.8, etc), and want to be certain that you are using the correct pip executable for your interpreter, you can run pip as a Python package via
python3 -m pip <args>
where python3 can be replaced by any interpreter path.
You can specify the Python version with a flag (pip install --python-version 3.6 ...), but you can't seem to specify whether you want to install a module that is 32-bit or 64-bit. I have some theories, but its behavior is unclear to me on this point.
To be more specific, I want to use PIP.exe to install a module in an embedded Python area (which does not have its own PIP.exe) using the -t flag to specify the location.
Update: What seems to be the case is that a 32-bit Pip installs to 32-bit Pythons, and a 64-bit Pip installs for 64-bit Pythons.
You can specify the Python version with a flag,
You cannot. Option -t for pip install sets the target directory, not Python version.
but you can't seem to specify whether you want to install a module that is 32-bit or 64-bit.
pip is a Python script, it runs under a Python interpreter and the interpreter certainly knows if it's 32- or 64-bit.
To be more specific, I want to use PIP.exe to install a module in an embedded Python area
You can download packages for a different (from the current Python/pip) hardware platform, OS and Python version but you cannot install them. To install packages you must have a compatible pip. So first thing is to install pip for said embedded Python.
If you're doing something like trying to pull a package off of PyPI using a local pip and dump them into a folder on something like a CircuitPython device, you could try the following:
pip install \
--no-compile \
--target /your/path \
--python-version=3.5 \
--implementation=py \
--no-binary=:all: \
--no-deps \
adafruit-circuitpython-bmp280
That will dump the sources (modules or packages) into /your/path. The --python-version only uses the tags in the package and doesn't actually check the sources. --implementation=py will force it to use pure-python packages
Older answer, but useful for some context
You can specify the Python version with a flag.
Can you clarify this? Are you talking about running something like python3.5 or python3.8? That's the name of the Python binary.
You may also be using Windows, where you can include -32 or -64 as a suffix to the executable in the shebang line. The line is processed by the py PEP-397 launcher.
To eliminate confusion, the best practice is to not run pip install ... but instead python -m pip install .... You're then assured that the pip and python are for the same installation (version, virtual environment, etc.)
For example, if I have Python 2.7, Python 3.5, and Python 3.8 installed, I might have the following 'unambiguous' binaries on my path
python2
python2.7
python3.5
python3.8
However, there's also
python
python3
pip
pip3
What those actually point is anyone's guess and could depend on the install order, how they were installed, and any manual path manipulations. In some setups, pip might install to (e.g.) the Python 3.5 version while python is Python 2.7. If that were the case, if you ran pip install requests then python -c "import requests" it would fail.
To tie it back to the launcher if you're on Windows, running py -3.7-32 myscript.py would run your script with the 32-bit version of Python 3.7, if it exists. If you needed to install a package for it, use py -3.7-32 -m pip install ... to ensure it is installed for it, rather than pip install ... which will go who-knows-where.
Hello. I am using CentOS-7. As you all, know CentOS-7's default installed python is 2.7.5. So I've installed python 3.4 by yum from epel repository. Now in terminal, "python" runs python 2.7.5 and "python3.4" runs python 3.4.
This is good. But the problem is...Today I've downloaded mysql connector for python from here, and installed using yum. After that, When I try to import module in python 3.4, below error had occurred.
Import Error: No module named 'mysql'
But in python 2.7.5, that module import properly.
What should I do to import that module in python 3.4?
When you install via pip, it's installing for the specific/default Python version. So when you installed mysql, it installed to 2.7.5. Use pip3.4 or pip3 to also install it for Py3.x.
Edit: Since you're not doing it via pip, the module you're downloading - you need to download and install again for the correct Python version. They have different installers listed per platform, by bitness (or source which is platform independent) & Pyversion.
The module you install from yum depends on the current python version.
So first download virtualenv which contains your required python version (in your case https://virtualenv.readthedocs.org/en/latest/ ).
After that activate this virtualenvironment like this -
Go to the folder where you installed virtualenv from terminal.
Type source virtualenv/bin/activate.
After this run yum install command for your desired mysql connector.
This will download the mysql connector which is compatible with python 3.4.
You don't. You reinstall it again either from a package (not the same package, a different one) or by building it from source for 3.4.
download virtualenv and set the python3.4 environment
download related module in this isolated environment
I'm trying to interface with an API using a python program that only works in 2.7 and not in 3.4 which is also installed on my machine. However, one of my program's dependencies is the requests module. I have requests available in my 3.4 environment, but in 2.7 import requests results in:
import error: no such module named requests
Many Stack Overflow Q&A's have reccomended installing requests for 2.7 using: pip2.7 install requests, but apparently I don't have pip2.7 because that results in:
'pip2.7' is not recognized as an internal or external command
Question: How can I get pip2.7 instead of my default pip? Is it a seperate version of pip or do I just need to tell my current version I'm trying to install for version 2.7 not 2.4?
Note: The suggested solution to use easy_install-2.7 -U pip to install pip2.7 doesn't run on my Windows7 maching, the command propmt prints : easy_instal-2.7 is not recognized as an internal or external command
By default 3.4 won't create the plain pip command but it can be enabled with an option which is presumably what has been done in your case. Since there are other names for the Python 3 pip you can safely overwrite it by installing the 2.7 pip on top of it with easy_install.
Since this is Windows, your Python binaries are kept in isolated directories so you should just ensure that the 2.7 directory is first in the path variable. Look in the <python root 2.7>\Scripts directory to see if you have an easy_install already. If not you need to install setuptools.
I recently tried to install a couple of python modules via easy_install on my mac.
Background:
I'm using OS X 10.6.8 and Python 2.7.2. If I run which python I get the following:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Problem:
If I try to install a package, say easy_install cssutils - it finds the package, downloads and says it's installed, but when I then open python and type import cssutils - it says the package isn't found.
This has happened with multiple packages, but they do however work if the package has it's own installer script and I run that.
How can I get easy_install to install the packages correctly?
Typing:
head -1 `which easy_install`
at the command line will show the interpreter that easy_install is using. If it doesn't match the python version you're using, you may need to install setup tools for the version of python you're using. Alternately, if the correct version of easy install is present, you can either run it by using an absolute path, reference the specific easy_install variant (e.g. easy_install-2.7), or update your path.