Pip Package not being recognized in Conda - python

I have a Conda environment in which I installed the KivyMD package via pip (inside the environment).
In a PyCharm project, I copied the example code from KivyMD documentation. This project has my conda environment as interpreter. The interpreter is working perfectly with this (other files, which use other packages) and other projects. But I keep getting errors when trying to run the file with this package.
The code from the example, that I'm trying to run (https://kivymd.readthedocs.io/en/latest/getting-started.html):
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
class MainApp(MDApp):
def build(self):
return MDLabel(text="Hello, World", halign="center")
MainApp().run()
"Error:
ModuleNotFoundError: No module named 'kivymd.app'; 'kivymd' is not a package"
I've tried uninstalling and reinstalling it again, but got the same problem. The package shows correctly both in "conda list" in the environment and in the PyCharm packages panel.
Both Python and Pip used by Conda are in folders inside the environment folder as well (checked with "where" command). I'm using Windows and I double checked that I have the latest Anaconda version.
I've tried running the file directly on command line (outside PyCharm), but got the same problem as well.
How can I fix this?

Firstly I assume you are using python 3.8
Secondly have you installed kivy first following instructions here: https://kivy.org/doc/stable/installation/installation-windows.html
I did this from the conda command line on windows, which you can access either by your usual method or straight from the conda navigator. Make sure you check that the environment you want to be running this in is active by running
conda info --envs
If this returns nothing then conda is not working, this should return something that looks like this, but with filepaths after each environment name.
indicates the active env so if the environment you want isn't active use this to activate it:
conda activate envname
If not go back and do this from within your conda env on command line
then run the following in your conda env to install kivyMD:
git clone https://github.com/kivymd/KivyMD.git --depth 1
cd KivyMD
pip install .
Provided you have no error messages, try compile your code to get it to run, I would recomend staying in the env activated command line to do this.

I could only make it work with the following steps:
(maybe not necessary) Create a new environment in Conda
Install Kivy from conda forge (conda install)
Installed Kivy again with pip (pip install)
installed KivyMD with pip (pip install)
(maybe not necessary) created a new project in PyCharm
setup the new environment as interpreter
There was some conflict that Conda was not recognizing the pip package to run the project (and to run only). Apparently it was solved by installing the required Kivy package twice, both as conda AND as pip package. It seems that, at least in my case, it was necessary to "bridge" the 2 installations, though I don't know why.

Related

"ModuleNotFoundError: No module named 'kivymd'" in .spec file

I already did pip install kivymd in my Python project. I also had the kivymd directory in my project.
I'm working with a Mac.
I created a spec file called "coinsnack4.spec" including the code below:
from kivymd import hooks_path as kivymd_hooks_path
However, when I try to package my python project with the spec file with the command:
pyinstaller -y --clean --windowed coinsnack4.spec
I got the error below:
File "coinsnack4.spec", line 3, in <module>
from kivymd import hooks_path as kivymd_hooks_path
ModuleNotFoundError: No module named 'kivymd'
I really don't know why this happens because I already pip install kivymd. I don't know what to do next and I would appreciate if anyone could help me with this error.
Thank you very much!
Why are you facing this issue?
The reason behind this is the concept of virtual environments in python. Each virtual environment is independent of the other. You can use different virtual environments, activate and deactivate them as per your project's requirements.
I would suggest you go through this doc once Python venv
As when you do a `pip install <SOME_PACKAGE> from your local terminal, it installs the package from into the default python environment and from the terminal itself (not pycharm terminal) if you try to execute the python program it will work fine but as soon as you switch to pycharm or any other IDE, it has it's own python environment set and that environment is unaware of what happened in the other python environment. So you need to install the pip package here also, in order to execute the same python program.
Solution:-
The first thing I would suggest is to install the package in the virtual environment that the pycharm is using. For that click on the Terminal icon the below bar of your pycharm window. then do run the below command :-
python3 -m pip install kivymd
If this doesn't work, try configuring the python environment in pycharm.
Below is how you can change or update your python interpreter in pycharm: -
Setting an existing Python interpreter
At any time, you can switch your Python interpreter either using the Python Interpreter selector or in the Project Settings/Preferences.
Creating a new Python interpreter
To add a new interpreter to the current project:
If you have a conda environment, follow the below steps: -
Or if you want to setup a new virtual environment, do as below: -
I think you installed pyinstaller not in project's virtualenv, just:
pip install pyinstaller
then problem will be fixed.

pip-installed package inside conda virtual environment is not isolated

I am developing a simple python package (on macOS 10.14) and I have problems in setting the instructions how to install it. Right now the package is not available anywhere yet, but think of it as a dummy "hello world" package with a dummy "hello world" function inside a dummy "hello world" module. Of course it has a proper setup.py script that would allow users to install and uninstall the package with pip.
When I install and test it myself everything works fine, the problem is not related to the package itself.
The issue is that I cannot make conda virtual environments and pip work together... Next to my setup.py script there is a environment.yaml file that specifies the dependancies required for my package. Based on this file I create a virtual environment with:
conda env create --prefix ENV -f environment.yaml
I have conda 4.7.12 with Python 3.7.3 inside so the virtual environment has it's own pip. So I activate the virtual environment and explicitly call the pip inside to install my package in the virtual environment:
/Users/my_name/Desktop/dev_dir/ENV/bin/pip install . --user
The installation is successful and the package can be imported. However when I deactivate the virtual environment with conda deactivate and run python interpreter from the conda base environment (version 3.6.9) I can still load my package! For some reason it is available outside of that particular virtual environment...
Later, when I run the 'inner' pip from conda base shell:
/Users/my_name/Desktop/dev_dir/ENV/bin/pip uninstall pkg
The removal seems to go through as well. I get a message:
Uninstalling pkg-0.0.0.9000:
Would remove:
/Users/my_name/.local/lib/python3.7/site-packages/pkg-0.0.0.9000.dist-info/*
/Users/my_name/.local/lib/python3.7/site-packages/pkg/*
Proceed (y/n)? y
Successfully uninstalled pkg-0.0.0.9000
suggesting that the package was indeed installed in a directory .local, outside conda virtual environments.
And the best for the last: even after this uninstallation when I run python interpreters (regardless of which environment from) and I try to import pkg it still works! when I then type pkg in the interpreter I get the path to my development directory:
>>> import pkg
>>> pkg
<module 'pkg' from '/Users/my_name/Desktop/dev_dir/pkg/__init__.py'>
Could someone please help me disentangle this mess? I would like to have my package installed inside the virtual environment, nicely isolated. And also - it should be gone after uninstallation, right?
PS. PYTHONPATH variable is never set anywhere at any stage, I have been checking that...
when I then type pkg in the interpreter I get the path to my development directory
This can only happen if:
You modified your PYTHONPATH to include /Users/my_name/Desktop/dev_dir which you didn't do
You are running the interpreter while you are in the folder /Users/my_name/Desktop/dev_dir, seems likely as you called it your development folder.
Check the output of print(sys.path), which lists all directories that are searched when doing import (standard locations + PYTHONPATH) and also print(os.getcwd()) as the current working directory is also searched
You tried installing your package to your activated conda environment using
/Users/my_name/Desktop/dev_dir/ENV/bin/pip install . --user
Looking at [the docs](https://pip.pypa.io/en/stable/reference/pip_install/#cmdoption-user] however:
--user
Install to the Python user install directory for your platform. Typically ~/.local/
So the --user option is messing with your intention to install into the currently active environment. But pip actually does that by default when run inside a virtual environment. So simply do:
conda activate <your envname>
pip install .
#FlyingTeller already correctly identified the issue. I just wanted to point out that you could further streamline your process by adding the installation for your package into your YAML definition. For example,
name: my_env
channels:
- defaults
dependencies:
- python=3.7.3
- pip
- pip:
- -e /Users/my_name/Desktop/dev_dir/pkg
This is also further in line with the best practices (see "Using Pip in a Conda Environment").
Just wanted to hopefully clear some up by telling you this keeps happen to many and if you forget the rule that is NO root install with conda, all rules for your files might change and suddenly it keeps asking for sudo AND fails. Conda = NO SUDO! Hope you got it fixed!
You have to add the pip package to your environment (see https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html), otherwise the packages will be installed by the global pip installation such that those packages can be accessed by all environments.
Therefore, create an environment using
conda create --name exampleenv pip
instead of
conda create --name exampleenv

package installed in anaconda\lib\site-packages and found in spyder, but not in a jupyter notebook

I have installed the azureml package and can see it in . . .anaconda\lib\site-packages:
If I run import azureml.dataprep as dprep in a python script in Spyder (launched from Anaconda Navigator), it works. But, if I open one of my anaconda environments with jupyter notebook and try running the same line of code, I get an error about module not found for azureml:
I thought perhaps the problem was that the package needed to be installed for that specific environment, but azureml is not available as a package for install via the anaconda environments > install packages interface (there is an azure package but not an azureml package).
So, I followed instructions to use conda prompt to install a package into a specific environment. Instructions I followed:
(those are from this link)
And here is the result of following the instructions (it looked like it installed the package into the env):
But, I got the exact same error when trying to import the package in the environment started as a jupyter notebook. Then, I closed anaconda navigator completely just in case, but that also didn't change the result.
Any ideas about what I'm either doing wrong or how I can manually install this package into a specific anaconda environment?
You did the right thing to install the package into the environment. Btw, pip is automatically installed by conda into any environment that has Python, so installing it shouldn't have been necessary.
Are you sure that the environment that you installed into is the one in which your notebook kernel is running? Start the notebook and execute
!conda env list
That will give you a list of environments, and an asterisk * next to the one that is active.
You can also call pip directly from a notebook cell:
!pip install azureml
That will install into the conda environment in which the kernel is running.

How to Install a Package in PyCharm when project interpreter is set to conda, and the package is not provided/listed by conda?

I installed pycharm on my computer. I set the project interpreter to acaconda3/bin/python because that is the python3 interpreter I used on my computer before installing pycharm. I was able to install all packages I need using pycharm's package installer except for pydicom which is not provided by anaconda. However, pydicom, one of the packages I need for my project, is not provided by conda and hence does not show up in the list of available packages when I search.
How do I install this package that is not available with conda?
Open Anaconda navigator
Open environment from side tab
Open your environment which you created or choose the default( seems
in this case)
Choose Open in terminal
Run pip command here.
OR run pip by going to directory anaconda3/Scripts directory
Since pydicom is supported by conda-forge channel it wont show up on Pycharm unless you add that channel to conda environment channels manually.
Run below command for the environment
conda config --add channels conda-forge
Then it should show up in Pycharm.
Once channel added you can run below command within environment
conda install pydicom
Reference:
pydicom.github
conda-forge
so/using-anaconda-within-pycharm
Using PyCharm 2020.2 I can do this without going to the terminal or Anaconda.
Go to "Settings->Project->Python Interpreter" (same place as VictorLegros went, but the UI is different now: note the + button at the bottom of the list of packages)
Hit the + button, search for your package in the new dialog and then click "Install Package"
I double checked in the Anaconda UI afterward, and - after click on Update Index.. and waiting a bit (not 100% sure that was necessary, but I didn't see it at first) - I can now see the package "scikit-learn" that installed via PyCharm.
(Note: I'm not using Anaconda to do anything but verify: the search and installation was all in PyCharm)
I had this problem and I figured out that from the python interpreter dialogue I had to click the green circular Conda icon to disable "Use Conda Package Manager" (above the list of packages). Then when I clicked to add a package, I found all the packages I needed, which I presume were installed with pip.
I was also able to install packages that Conda needed to handle like psycopg2. Hope this helps.
I don't know if it's identical on the Mac, but for Win 10 Pycharm, you can access the Terminal from:
View > Tool Windows > Terminal (Alt+F12)
From there, make sure the correct conda environment is active through:
conda activate <your_env>
Then, you can install a package as one normally would typing in the command line, e.g.
conda install -c conda-forge <some_thing>
The exact command changes if you're using pip or some other manager or repository location, but it's helpful to do it this way if you want to stay in the IDE.
Also, you can verify the package in present in File > Settings > Project: ... > Python Interpreter
You should see your manually added package listed here, even though you didn't install it through the GUI.

Package installed by Conda, Python cannot find it

I try to install Theano by Anaconda. It works, but when I enter the python -i, import theano shows No module named 'theano'. Do I need to switch another interpreter of Python, how? Also, for the packages installed by conda, if I don't double install them, can I find in Python? How is Python related to Python by Anaconda? Thanks!!!
I had have a similar issue, trying to install folium. If you are using the Anaconda:
When you install using conda install -c conda-forge folium, the package will be placed in:
./anaconda3/envs/[name env]/lib/python3.7/site-packages/folium
When you install using pip (with a anaconda env activated), pip install folium, the package will be placed in:
./anaconda3/lib/python3.7/site-packages/folium
Python use first the sites-packages as the target directory of manually built python packages. When you build and install python packages from source (using distutils, probably by executing python setup.py install ), you will find the installed modules in site-packages by default.
In this case you have two places: /anaconda3/lib/python3.7/site-packages/ and /anaconda3/envs/[name env]/lib/python3.7/site-packages/.
First the modules will be available as default in /anaconda3/lib/python3.7/site-packages/. Sometimes (and I really don't know why) the modules inside sites-packages conda env are not available to import automatically without export the PATH.
So, to solve this issue, you have 2 options:
Installing using pip install folium and import folium (don't need install by conda install), or
After conda install , run conda init, close the terminal and open a new one. So, try to import again.
Here are some tips about use a pip in a conda-environment.
You can refer to a specific version of python by using the following at the first line of your .py file
This is for python 2.7
#!/usr/bin/env python2.7
This is for python 3
#!/usr/bin/env python3
As other users already pointed out you need to check if your module is included in your sys path. Use code:
import sys
print(sys.path)
If not you can include this in your sys.path by using the command:
sys.path.append('/path/to/the/folder/of/your/module/file')
or place it in default PYTHONPATH itself.
Other great answers:
https://stackoverflow.com/a/19305076/5381704
The problem is that in the code editor you are using, you are running the default interpreter. Based on your code editor, change the python interpreter to the conda interpreter and it will work.
In my case that happened because conda screwed up the environment variables. Instead of using env-specific python and pip, it used the globally installed ones.
Solution:
conda deactivate your-env
conda activate your-env
In my workstation, I was able to solve No module named <module name> error using two different ways.
First method, I solved this temporarily by:
(1) Open a Terminal
(2) $ conda activate <Conda environment name>
(3) $ export PYTHONPATH=/home/<user name>/anaconda3/envs/<Conda environment name>/lib/<Python package version>/site-packages:$PYTHONPATH
It is a temporary solution. Whenever you run your virtual environment, you have to do this.
My runtime environment:
    OS: Unbuntu 18.04
    Conda version: 4.8.2
    Conda-build version: 3.18,11
    Python version 3.7.6.final.0
Second method, I removed the
alias python=/usr/bin/python3.6 line in bashrc file.
Somehow this line blocks using Python tools installed in Anaconda Virtual Environment if the Python version in the Virtual Environment is different.

Categories

Resources