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

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.

Related

Why can't Python find the module I installed?

Presently I'm using Python on a Windows system. I installed Python 3.10 from Anaconda and also the Pycharm IDE. I have ensured that Python is in the correct path in the environment variable. I have also replicated this problem using two different versions of Python, 3.10 and 3.9.
Very simply, in PyCharm, I open a terminal and type
conda install -c numpy numpy.
Then, I write a new "main.py" script. I have one line: "import numpy". I receive the error:
Traceback (most recent call last):
File "C:\Users\---\PycharmProjects\pythonProject3\main.py", line 17, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
What am I doing wrong?
Going on advice from a friend, I created a new PyCharm project sitting not in my user directory but on the C: drive, and got the same error. Finally, when trying to re-install the package using either using either pip or conda, I get this message:
# All requested packages already installed.
You have 2 versions of Python:
Default Python (used everytime you open your command prompt and type python or python3)
Anaconda is installing packages in a virtual environment, using it's own Python (it is located in a different path)
You can see the path of your installed python using python -c "import os, sys; print(os.path.dirname(sys.executable))"
You have 2 Options:
Configure the PyCharm in order to use the anaconda Python. https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#view_list
Open a command prompt in the project's folder (you can do it easily using PyCharm). Type conda env list. This will show you all available anaconda virtual environments. Choose 1 of them and type conda activate <env_name>, where <env_name>=the name of the environment. Then, run your program using python <name_of_your_program>
You can see the paths where the anaconda environments and packages are installed using conda info
There main reason for this is
You are running your main.py in different environment rather than where you installed numpy.
If you trying to run it via cmd use this method
Check which environment you are in right now. refer this and
this. But the most easiest way to do this is use where command
in windows cmd. C:\> where python or C:\> where python3. You will
get the path of activated interpreter.
list conda envs - conda env list
activate conda env - conda activate <env name>
then run this command. pip freeze . and check is there numpy in
the list. If not you have to find and activate the environment where
you have installed numpy.
If you want to run it in pycharm
Refer this on how to change pycharm interpreter.
https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html
Many things can cause this, usually its one of these
You may have to restart your terminal, or IDE if running in there, after installing a package to "refresh" the environmental path
The package is not in the environmental path

ModuleNotFoundError: No module named 'azure.cognitiveservices.vision.customvision'

I am trying to use the microsoft azure custom vision service on a mac from Jupyter in VS Code
I have Python 3.8.3 installed.
I have done pip install azure.cognitiveservices.vision.customvision and confirmed it is there using pip show.
When I execute the command
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
I get the error:
ModuleNotFoundError: No module named 'azure.cognitiveservices.vision.customvision'
I have tried adding the location where the package is installed to $PATH but that does not fix the problem.
Any thoughts gratefully received! thx
It is recommended that you always create and activate a python virtual environment to work with Jupyter notebooks, like an Anaconda environment, or any other environment in which you've installed the Jupyter package.
To select an environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Once the appropriate environment is activated, you can create and open a Jupyter Notebook and connect to a remote Jupyter server for running code cells. Check Working with Jupyter Notebooks in Visual Studio Code for more info.
This is true for application development in Python in general as well.
A virtual environment is a folder within a project that isolates a copy of a specific Python interpreter. Once you activate that environment (which Visual Studio Code does automatically), running pip install installs a library into that environment only.
When you then run your Python code, it runs in the environment's exact context with specific versions of every library. You can create a requirements.txt file for the libraries you need, then use pip install -r requirements.txt.
Here is a snippet from a sample requirements.txt file:
azure-mgmt-core==1.2.0
azure-mgmt-network==16.0.0
azure-mgmt-resource==10.2.0
If you don't use a virtual environment, then Python runs in its global environment that is shared by any number of projects.
Refer to the Azure SDK for Python Developer docs for more information on configuring your local Python dev environment for Azure.
Whenever U get: ModuleNotFoundError, the simple solution is to install the module using
pip install (module name)
For example, in your case try to run the following line:
!pip install azure
The ! is to run a command in a notebook.

Installed Python 3, now my Anaconda environment is messed up

I apologize in advance for my poor vocabulary - I do not know much about environments, paths, and things of the sort. I am on macOS Catalina.
I created a program using Spyder from Anaconda. My program uses packages like pandas and numpy which are built into Anaconda. I used to be able to run the program in Terminal with the command: $ python3 app.py.
However, I recently downloaded Python 3.9.1 from https://www.python.org/downloads/mac-osx/. Ever since then, I have been unable to run the program in Terminal because of missing package errors like
import numpy as np
ModuleNotFoundError: No module named 'numpy'
I went to my applications folder, right clicked on Python 3.9.1, and clicked Move To Trash. This did not solve my problem. I reinstalled Anaconda (I did not uninstall it - just simply installed again), but that also did not solve my problem. I am able to run the program in Spyder (from Anaconda), but I wish to run it in Terminal again.
I believe the solution is simple, but I am not sure what to do. I have tried searching and reading but I am not familiar with the terminology. I think I just need to reset the environment, but I am not sure.
Anaconda is used for creating closed enviorments, so you don't need to thrash your computer with global packages.
Imagine you have 2 different projects. Project A works only on python 3.2 and Project B works on 3.8.
That's where anaconda comes in.Managing enviorments with conda
conda create -n PROJECTA python=3.2
conda create -n PROJECTB python=3.8
Now activate env you wish to work with. For macOS
source activate PROJECTA
Now you should see (PROJECTA) instead of (base)
Now inside this PROJECTA you can install modules you require like
pip install numpy
and when executing .py file
move to dir with your app.py file and
python3 app.py
this will be opened in conda enviorment you created and activated, using modules you installed in this env.
You can still edit py file and execute it through shell, but it will throw errors if you try to run it from IDE without linking project to respected conda enviorment.
If you are using PyCharm Configure a Conda vir env in PyCharm
You must create separate environments for every projects or it will get messed up.
conda create -n name_of_environment python=3.6
You must not delete the python folder into trash rather uninstall it Python 3.9.1 and remove its path from the directory.
In short if you run python from terminal and it is not supporting your Installed anaconda packages . You should use anaconda prompt instead of CMD.
Because your anaconda is not added to path rather then it picks up the python 3.9 you have installed from Python 3.9.1 from https://www.python.org/downloads/mac-osx/. ( This is the python with separate environment then anaconda so it wont detect anaconda packages ).
When you run the cmd and enter python it runs the python that you downloaded and installed rather then Anaconda
At the time of installing anaconda it gives option to add conda variables to path you can select those at installing stage / or add manually

Pip Package not being recognized in Conda

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.

Can't import numpy module into python file, but it works with terminal

On Ubuntu I have python 3.6.9
I created new project using PyCharm, into project folder there is venv folder.
When I open venv folder in terminal and do:
python3
import numpy
It works, no any error.
But when I create some file in that venv folder using PyCharm, and try in this file:
import numpy
then I get ModuleNotFoundError: No module named 'numpy' error.
Why can't import module into file? module obviously installed because I can import that using terminal (folder is same for both cases).
What is my mistake ?
Pycharm uses virtual environments for serving modules, so you will have to download the package from either the GUI or by activating the virtual environment in the terminal and then doing the pip install.
if you want to do it from the GUI you can check the official tutorial. (Working and tested)
To do it manually, first to activate the environment in linux go wherever the venv folder is and type source venv/scripts/activate (to activate) and then you'll be able to do pip install numpy. (Not tested but it should work)
Could you share your Pycharm configuration?
Some extra steps are needed in Pycharm to configure venv, otherwise it will just take your normal interpreter site-packages.
Take a look at the Pycharm help page: https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html#existing-environment
looks like you need to install numpy through pip3.
Also, check which version of python is running in pycharm.

Categories

Resources