installed conda package fails to import - python

Im using anaconda to install packages and manage my environments on macOS Mojave 10.14.2.
I installed a new package wget at the terminal
conda install wget
When I run conda list the package shows up and I get no errors.
However, when I run
import wget
In spyder or jupyter notebook I get and error:
File "<ipython-input-28-1d5f1c9c9a6b>", line 7, in <module> import
wget ModuleNotFoundError: No module named 'wget'
The package also shows up in my base(root) environment, so I have no idea why spyder or jupyter notebook can't find the package.

Your command conda install wget installs wget for use at the conda prompt. To install wget for import in Python, you need to install a different package:
conda install -c anaconda pywget
In Python it's still:
import wget

in conda prompt run this command and import , it will work
conda install -c anaconda wget

Related

ModuleNotFoundError in jupyter notebook when importing a module after conda install

I'm trying to use conda to set up one of my projects. I installed openCV by conda install -c conda-forge opencv. When I run conda list, I can see openCV in the list. Running python -i and then import cv2 works, but when I open up Jupyter Notebook and navigate to that folder (I have to do it this way because running jupyter notebook in the directory also pulls up an error), and open up a notebook which imports cv2, I get an error. Why is this happening, and how would I solve it? Any kind of help will be greatly appreciated.
So as I said before, I wasn't able to start Jupyter Notebook from the command line, I had to start it from the start menu and navigate to my folder. Because of that, my notebook wasn't working in the conda environment that I created. I fixed that by running python -m ipykernal install --user --name <env_name> --display-name "<display_name>". I had to conda install ipykernel. It works now. Thanks for the other answers.
Usually that indicates that the notebook is running with a different Python or in a different environment from Python in the command prompt. Check sys.executable to see which Python it's running in, and sys.path to see where it's looking for imports
Everybody says that pip installing from notebook is not the best practice, but maybe for a fast try it would do the thing:
# Install a conda package in the current Jupyter kernel
import sys
!conda install --yes --prefix {sys.prefix} packagename
# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install packagename
I used it from Install python packages on Jupyter Notebook and it worked for me.
Step 1: Activate environment before running
conda activate <environment-name>
Step 2: Install ipykernel
conda install -c anaconda ipykernel
Step 3: Add the conda environment to ipykernel
ipython kernel install --name <environment-name> --user
Step 4: Install your package
conda install -c conda-forge opencv
References:
How to add your Conda environment to your jupyter notebook in just 4 steps
Conda environments not showing up in Jupyter Notebook

ModuleNotFoundError: No module named 'tensorflow_docs' when creating TensorFlow docs

I'm trying to follow the contribution guide for documentation. The required steps are:
git clone https://github.com/tensorflow/tensorflow tensorflow
cd tensorflow/tensorflow/tools/docs
pip install tensorflow==2.0.0-alpha0
python generate2.py --output_dir=/tmp/out
But the last command gives me:
Traceback (most recent call last): File "generate2.py", line 36, in
from tensorflow_docs.api_generator import doc_controls ModuleNotFoundError: No module named 'tensorflow_docs'
This is in line 36 of generate2.py:
from tensorflow_docs.api_generator import doc_controls
I haven't found a pip package containing tensorflow_docs. Any ideas?
First install tensorflow_docs using this command:
pip install git+https://github.com/tensorflow/docs
first, you need to install git.
install git by using this command conda install git in anaconda prompt. then run the following command
!pip install -q git+https://github.com/tensorflow/docs
in jupyter notebook
OR
pip install -q git+https://github.com/tensorflow/docs
in anaconda prompt.
I have a try and figure out how to solve the problem. Since I have multuple Python versions by checking "conda list" and "pip list", either the Jupyter or IDE installation method could not install tensorflow_docs into the correct directory.
1.For Jupyter
$ conda install git
$ !pip install -q git+https://github.com/tensorflow/docs
2.For IDE
$ conda install git
$ pip install -q git+https://github.com/tensorflow/docs
The problem is that pip has installed tensorflow_docs into the default directory of Python 3.6 with the following path: /home/user/local/lib/python3.6/site-packages. However, I use Python 3.9 in the conda environment in the most of the time.
3.Copy tensorflow_docs into Python 3.9 site-packages
It is applicable after putting tensorflow_docs to the designated directory of Python 3.9. So I can use tensorflow_docs after the following operations:
First.Download tensorflow_docs
https://github.com/tensorflow/docs/tree/master/tools/tensorflow_docs
Second.Save tensorflow_docs in Python 3.9 directory
/home/user/miniconda3/lib/python3.9/site-packages/tensorflow_docs

Can't install installed python packages

I am using Jupyter Notebook to install packages with the command: !pip install.
I just used this command to install the Options package, but it's still not showing up. I checked the default python Environment as well as the conda environment.
list of Python environments
To install packages into jupyter notebook you have to use conda instead of pip. Just find the conda install instruction for the package you are trying to install.
For example, the command to install numpy on conda is:
conda install -c anaconda numpy
instead of the regular pip version which would've been:
pip install numpy
You can search for yours by googling: conda install <package-name-you-want-to-install>.

'No module named spacy' in ipython, but works fine in regular python interpretter

I am currently trying to import spacy using Jupyter Notebooks and running into a problem. Every time I try to import it, it says that it cannot find the module, even though the regular python shell interpreter works just fine.
Information:
Conda Environment
installed using conda install -c conda-forge spacy
shows up in conda list | grep spacy
Jupyter can find other packages in the conda env, just not spacy
Thank you for any help you can provide.
EDIT: Terminal Commands:
1. cd into project directory
2. conda create -n <env name>
3. source activate <env name>
4. conda install -c conda-forge spacy
5. python -m spacy download en
6. python
- import spacy #works!
- nlp = spacy.load('en') #works!
- quit()
7. ipython
- import spacy
ModuleNotFoundError: No module named 'spacy'
EDIT2:
Figured it out. My sys.path was different in ipython and wasn't searching through the conda env. I had to run conda install jupyter in the env and then everything worked. Apparently the root jupyter doesn't detect if you're inside an environment or not.
Figured it out. My sys.path was different in ipython v python shell. ipython wasn't searching through the conda env.
I had to run conda install jupyter in the env and then everything worked. Apparently the root jupyter doesn't detect if you're inside an environment or not. This makes sense now that I know some more about the internals as it needs to identify with a specific ipykernel.
Hope this helps anyone else running into the same issue.
create virtualenv
activate it
install jupyter and spacy on virtualenv
pip install jupyter
pip install spacy
add kernel
pip install ipykernel
python -m ipykernel install --user --name=newkernelinvenv
start jupyter and load your new kernel
I faced a similar issue when I installed Spacy in Windows. I just had to run my command window as administrator.
Sometimes we make stupid mistakes :)
An alternative without installing Jupyter inside your venv is:
$ python -m venv projectname
$ source projectname/bin/activate
(venv) $ pip install ipykernel
(venv) $ ipython kernel install --user --name=projectname
(venv) $ conda install -c conda-forge spacy
(venv) $ jupyter notebook
Credits: https://anbasile.github.io/programming/2017/06/25/jupyter-venv/

How to install pyperclip to Anaconda?

I want to install pyperclip from GitHub using pip install but unfortunately it's only installed to pip and not Anaconda. I'm using PyCharm and my interpreter is Anaconda. (Linux)
I want Anaconda to install the package 'pyperclip' as well so I can use it in PyCharm. I've try Googling but can't find any result.
From Anaconda's website, run the following in Terminal/cmd:
conda install -c conda-forge pyperclip
You need to make sure the following command uses the pip in your CONDA_FOLDER/Scripts/pip or CONDA_FOLDER/envs/ENVIRONMENT_NAME/Scripts/pip:
pip install git+https://github.com/asweigart/pyperclip.git
or if a specific branch (instead of the default one is needed) then use:
pip install git+https://github.com/asweigart/pyperclip.git#BRANCHNAME
However you need to make sure your PyCharm actually uses the environment in which you install the package! Otherwise there is no sense in installing it in the wrong one.
I am not sure because I am not running linux and I never tried it but according to the official website
run anaconda prompt
and then type: conda install -c auto pyperclip=1.3
for more info: https://anaconda.org/auto/pyperclip
To install pyperclip from conda run following command.
conda install -c bryanwweber pyperclip
I was having a hard time installing pyperclip on Anaconda Jupyter notebook. I installed pip and also upgraded it to the latest version. I followed your answers but didn't have luck.
If you're a Windows user, here's how I resolved:
1) Open Anaconda terminal (it's your Windows terminal inside Anaconda)
2)It will open showing the C drive and your username like: C:\Users\joe.jacob
3)C:\Users\joe.jacob\ dir (this will show you where you have Jupyter Notebook. Mine was at .jupyter, so I navigated to it using: cd .jupyter
C:\Users\joe.jacob\cd .jupyter
4)Now install pyperclip at your jupyter notebook like this:
C:\Users\joe.jacob\.jupyter>pip install pyperclip
5) It installs pyperclip package. Installing other pip packages is similar.
I hope this helps.
search for anaconda prompt on your computer and type one of the following
conda install -c conda-forge pyperclip
conda install -c "conda-forge/label/gcc7" pyperclip
conda install -c "conda-forge/label/cf201901" pyperclip
conda install -c "conda-forge/label/cf202003" pyperclip
then click enter and wait for it to download you can repeat it again to confirm whether its installed

Categories