I am unable to install a module called 'jieba' in Python 3 which is running in Jupyter Notebook 6.0.0. I keep getting ModuleNotFoundError: No module named 'jieba' after trying these methods:
1. import jieba
2. pip3 install jieba
Can anyone help? Thank you.
pip3 in the terminal is almost certainly installing your package into a different Python installation.
Rather than have you hunt around for the right installation, you can use Python and Jupyter themselves to ensure you are using the correct Python binary. This relies on three tricks:
You can execute the pip command-line tool as a module by running python -m pip .... This uses the pip module installed for the python command, so you don't have to verify what python installation the pip3 command is tied to.
You can get the path of the current Python interpreter with the sys.executable attribute.
You can execute shell commands from Jupyter notebooks by prefixing the shell command with !, and you can insert values generated with Python code with {expression}
You can combine these to run pip (to install packages or run other commands) against the current Python installation, from Jupyter itself, by adding this into a cell:
import sys
!{sys.executable} -m pip <pip command line options>
To install your jieba package, that makes:
import sys
!{sys.executable} -m pip install jieba
If you are using Anaconda, then you could also install the conda package for jieba; the package does not require any platform-specific dependencies or compilation, but it may be more convenient for you or necessary to install other packages that do have such requirements and have pre-compiled conda packages.
In that case, tell the conda command about your Python executable:
import sys
!conda install --yes --prefix {sys.prefix} <package name>
Related
I bought a book which comes with jupyter notebook. In the first chapter, it asks me to install required libraries. It use {sys.executable} -m. I never see it before. what does {sys.executable} and -m do? also why use --user at the end?
typically, I just use ! pip install numpy==1.19.2
Anyone can help me understand it? Thank you!
import sys
!{sys.executable} -m pip install numpy==1.19.2 --user
!{sys.executable} -m pip install scipy==1.6.2 --user
!{sys.executable} -m pip install tensorflow==2.4.0 --user
!{sys.executable} -m pip install tensorflow-probability==0.11.0 --user
!{sys.executable} -m pip install scikit-learn==0.24.1 --user
!{sys.executable} -m pip install statsmodels==0.12.2 --user
!{sys.executable} -m pip install ta --user
Let's split this question up into multiple parts.
Part 1
From the Python documentation:
sys.executable
A string giving the absolute path of the executable binary for the Python interpreter, on systems where this makes sense. If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.
Formatting that in, we get:
...\python.exe -m pip install <package> --user
Part 2
Also from the docs:
-m <module-name>
Search sys.path for the named module and execute its contents as the __main__ module.
This is generally the same as just pip install <package> --user, however if you have multiple versions of Python installed, the wrong version of pip might get invoked. By using -m, a matching version of pip will always be invoked.
Part 3
This time from the pip documentation:
Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on Windows. (See the Python documentation for site.USER_BASE for full details.)
Basically, this means that instead of installing to the normal package directory (which could require administrator privileges), it installs to %APPDATA%\Python, which should always be accessible as it is in your user folder.
sys.executable is refering to the Python interpreter for the current system. It comes handy when using virtual environments and have several interpreters on the same machine.
The -m option loads and execute a module as a script, here pip.
The --user is an option for pip install, see this answer describing its use.
Then the !{} is jupyter-specific syntax to execute commands in a cell if I remember correctly.
I installed the python package lifelines on my terminal. The windows terminal is my terminal of choice, with a powershell and anaconda terminal that I often used.
I tried installing the package using the provided commands in the documentation:
pip install lifelines
and
conda install -c conda-forge lifelines
Both times the installation is marked as successfull. When I run Python within the terminal I can import the lifelines package without problem. However whem I import it on a jupyter notebook it yields a ModuleNotFoundError.
The base environment I use does not contain the lifelines package when I verify its contents using the Anaconda Navigator.
The jupyter notebooks are run on Anaconda Powershell, and so are the environments and packages.
Installing on the Windows Powershell will never work. Running the conda install -c conda-forge lifelines in the Anaconda shell solved the issue.
So silly, yet so time consuming it is worth sharing.
I had a issue like this, python3 -m pip install <package_name> solved it for me. Use python -m pip install <package_name> for Python2.
I wanted to make a voice assistant importing SpeechRecognition on Python;
I installed PyAudio and SpeechRecognition with the following commands: pip install PyAudio pip install SpeechRecognition on Linux, but when I try to import SpeechRecognition with import speech_recognition as sr, VSCodium doesn't import the module.
But then I tried to import other modules, and the same thing happens... what should I do?
You can check which pip you are using through pip --version, and which python you are using through where python(cmd) or get-command python(PowerShell). Make sure pip matches up with the python interpreter you are using.
If it's not, you can try to reopen the terminal with the shortcut 'Ctrl+Shift+`'. And then to check the pip version again.
If it still not matches up with the interpreter. That's maybe this environment hasn't installed the pip in this environment.
I think its main reason is on Linux pip is for python 2 while pip3 is for python 3.
If you are using py3 using pip3 may help. Other reasons may include a virtual environment you are on.If vscodium does not automatically activate it or whatever extension you are using do not run in the virtual environment it may not find the packages you installed. you can try these commands:
Example:
pip3 install PyAudio
pip3 install SpeechRecognition
python3 main.py
I am trying to run this tutorial
https://learn.microsoft.com/en-US/azure/data-factory/quickstart-create-data-factory-python
but I fail to install the packages. I tried several installations but I keep getting the error No module named 'azure.mgmt.datafactory' when trying to run from azure.mgmt.datafactory import DataFactoryManagementClient.
I am using anaconda and windows 10.
I tried running the recommended anaconda packages https://anaconda.org/anaconda/azure and https://anaconda.org/clinicalgraphics/azure-mgmt-resource under a python 3.5 environment and I also tried to manually install everything from github (https://github.com/Azure/azure-sdk-for-python) using
git clone git://github.com/Azure/azure-sdk-for-python.git
cd azure-sdk-for-python
python setup.py install
In both the normal (Python 3.6) and the new (Python 3.5, using Anaconda version with Python 3.5) environment. None of this worked.
What am I missing?
(Note that from azure.mgmt.resource import ResourceManagementClient worked fine with the anaconda installation)
EDIT
After the first response, I ran the following commands from the powershell
pip install azure-mgmt-resource
pip install azure-mgmt-datafactory
pip install azure-mgmt
which resulted in ModuleNotFoundError: No module named 'azure.mgmt'
Uninstalling the three packages and installing azure-mgmt as a first one did not solve the issue either. However, I don't know how to uninstall the manually installed package from python setup.py install, which still might be an issue.
Have you tried pip install in powershell/cmd?
pip install azure-mgmt-datafactory
Update (Jan's answer):
pip freeze > requirements.txt
pip uninstall -r requirements.txt
python -m pip install azure-common
python -m pip install azure-mgmt
python -m pip install azure-mgmt-datafactory (this might not be needed as it comes with azure-mgmt)
Ok, this is how I got the required azure libraries to work (thx to Saul Cruy, who gave me the idea)
Using this post What is the easiest way to remove all packages installed by pip?, I created a requirements file in PowerShell
pip freeze > requirements.txt
In this file, I manually kept only the entries with azure.
Then, I deleted all packages in the file
pip uninstall -r requirements.txt
The steps above were repeated twice, as upon first delete, some azure packages survived.
Then, I ran (all in PowerShell, in that order)
python -m pip install azure-common
python -m pip install azure-mgmt
python -m pip install azure-mgmt-datafactory
The reason might(!) be that installing packages in the anaconda console using the conda commands causes confusion in the dependencies (I tried a similar approach in a conda environment as it seemed like a good idea to seperate the azure packages from the other ones, but without success).
I use Spyder, with Python 2.7, on a windows 10. I was able to install the PyPDF2 package with a conda command from my prompt. I said installation complete. Yet, If I try to run a simple import command:
import PyPDF2
I get the error:
ImportError: No module named PyPDF2
How can I fix this?
In my case, I was trying to import 'pyPdf2' instead of 'PyPDF2'. Observe the case.
import PyPDF2
is correct.
If you use python3 maybe
apt-get install python3-pypdf2
I faced the same problem. But, In my case,
I previously installed Python3 separately from official website and was using without any issues
Then later I installed Anaconda package distribution software which itself has another Python3 installed in corresponding directory.
So, when I installed PyPDF2, it installed normally and while importing throws an error, because the base path of python3 was changed to be used with Anaconda.
Then I opened Anaconda prompt and installed PyPDF2 there and tried to import. It worked!!
Then I can use it from any command prompt in my Windows PC. Or else you can delete Anaconda and everything works normally. Its just a conflict of two pythons in my pc.
Conclusion: Try any overlapping softwares in your PC(in my case Anaconda prompt) and try their CMD to install packages and import. If I wanted to install any package I have to go to Anaconda prompt and install it and importing that modules works anywhere without any error. So from now on wards I'm only using Anaconda prompt as my default installation prompt.
This is the case which I followed for python3. For python2 try with pip:
pip install PyPDF2
I had this problem too when I tried to import PyPDF2 like this:
sudo apt-get install python-pypdf2
When running some simple script with import PyPDF2, I would get an error like this:
ImportError: No module named PyPDF2
The solution was to also install pdfmerge, like this:
pip install pdfmerge
How to install Python packages on Windows, Mac, and Linux for various versions of Python which are simultaneously installed:
I have multiple versions of Python installed on my Windows 8.1 machine (Python 2.7, 3.5, and 3.7). This created problems (confusion, I should say). You must therefore be very explicit when installing packages. Ex:
py -3.7 -m pip install PyPDF2 # on Windows
python3.7 -m pip install PyPDF2 # on Mac and Linux
INSTEAD OF the more generic:
pip install PyPDF2 or
pip3 install PyPDF2
And to upgrade pip, be very specific in your python version, like this:
py -3.7 -m pip install --upgrade pip # on Windows
python3.7 -m pip install --upgrade pip # on Mac and Linux
INSTEAD OF the more generic:
py -3 -m pip install --upgrade pip # on Windows
python3 -m pip install --upgrade pip # on Mac and Linux
Now, I can run python 3.7 with py -3.7 on Windows, or with python3.7 on Linux, and since I did py -3.7 -m pip install PyPDF2 on Windows, or python3.7 -m pip install PyPDF2 on Linux or Mac, the import PyPDF2 command works! Previously, since I had only done pip3 install PyPDF2, the import PyPDF2 command only worked if I ran py -3.5 on Windows or python3.5 on Linux, oddly enough, since apparently that was my "default Python3 version" which the more generic pip3 install PyPDF2 command must have installed the PyPDF2 module into. I think it has something to do with the fact that I installed Python 3.5 for all users, but Python 3.7 for only my user account, so the different pip install commands were placing the installed packages into different locations, with the 3.5 version being the "default" Python3 install location.
See more here: https://docs.python.org/3/installing/index.html:
... work with multiple versions of Python installed in parallel?
On Linux, Mac OS X, and other POSIX systems, use the versioned Python commands in combination with the -m switch to run the appropriate copy of pip:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
Appropriately versioned pip commands may also be available.
On Windows, use the py Python launcher in combination with the -m switch:
py -2 -m pip install SomePackage # default Python 2
py -2.7 -m pip install SomePackage # specifically Python 2.7
py -3 -m pip install SomePackage # default Python 3
py -3.4 -m pip install SomePackage # specifically Python 3.4
When using pip, it usually gets installed in Python 2+ so try
pip3 install PyPDF2
I had the same issue and fixed it when switching Python compiler (bottom left corner on Visual Studio Code) . Try on different versions and eventually it should work.
Im following a UDEMY course here. Im using Anaconda prompt and jupyter notebook.
I encountered the same issue as OP. What I did to have the library working:
restart the environment
go to your anaconda prompt
control c to stop the running instance
conda activate ***your_env_here***
pip install PyPDF2
(in my case open the jupyer notebook) jupyter notebook
You can now import the library without the error. import PyPDF2
Hope this works for you.
I encountered the same issue today while doing Udemy course.
try the following:
type this
import sys
!{sys.executable} -m pip install PyPDF2
then
import PyPDF2
Hope it works for you too.