2

I've installed Kivy and all the need files as far as I know, but I'm still getting this error message and I don't know why.

from kivy.app import App
from kivy.uix.gridlayout import GridLayout


class Container(GridLayout):
    pass


class MainApp(App):

    def build(self):
        self.title = 'Awesome app!!!'
        return Container()

if __name__ == "__main__":
    app = MainApp()
    app.run()

This is the error message I get:

Traceback (most recent call last):
  File "C:\Users\Yassi\OneDrive\Afbeeldingen\Bureaublad\main.py", line 1, in <module>
    from kivy.app import App
ImportError: No module named 'kivy'

I installed Kivy through anaconda, so the files for kivy might be installed in a wrong directory. Either way, I don't know how to fix this problem.

I'm running this program on a Windows 10 OS and I use python-3

Edit: It might have something to do with how I refer to the python interpreter. I can't find the location of the python interpreter though so now I'm stuck.

Edit2: This is the place where I installed python: C:\Program Files\IBM\SPSS\Statistics\25\Python3. Any way how I can refer to this? I think this is where the problem lies.

Y. Ben
  • 73
  • 1
  • 2
  • 9
  • Do you have multiple versions of python installed? Also you may want to activate your environment with source activate [name of env] or conda activate [name of env] – cvanelteren Dec 08 '18 at 20:46
  • I entered conda activate base (the only environment in C:\Users\Yassi) and it didn't help. I also have Python 2.7 installed yes – Y. Ben Dec 08 '18 at 20:54
  • there are so, so *so* many reasons a module might not be able to be imported, I can't even guess what it could be. – Pika Supports Ukraine Dec 08 '18 at 20:55
  • @Y.Ben Simply run CMD there with admin rights, and install traditionally kivy via pip, or totally reinstall python. – Zydnar Dec 09 '18 at 15:05
  • But everytime it installs the files on the same place so it doesn't solve the problem – Y. Ben Dec 09 '18 at 22:23

7 Answers7

2

Check if in current enviroment you have kivy installed:

import pip._internal as pip

print([i.key for i in pip.get_installed_distributions()]
# or 
pip.main(['freeze'])

So you will see if in this enviroment you have kivy installed. To make sure you have kivy installed in this enviroment you can write something like this:

try:
    from kivy.app import App
except ImportError:
    import pip._internal as pip
    pip.main(['install', 'kivy'])
    from kivy.app import App
Zydnar
  • 1,472
  • 18
  • 27
  • This is the error message I get when I run your second piece of code Traceback (most recent call last): File "C:/Users/Yassi/OneDrive/Afbeeldingen/Bureaublad/ddd.py", line 2, in from kivy.app import App ImportError: No module named 'kivy' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Users/Yassi/OneDrive/Afbeeldingen/Bureaublad/ddd.py", line 4, in import pip._internal as pip ImportError: No module named 'pip._internal' – Y. Ben Dec 08 '18 at 21:03
  • If `pip._internal` is wrong import this means you have older pip than version 10.0, so either use simply `import pip` or update pip first. – Zydnar Dec 09 '18 at 08:53
  • It didn't help. – Y. Ben Dec 09 '18 at 13:10
  • It might has something to do with the python interpreter, but I still don't know how to solve the problem – Y. Ben Dec 09 '18 at 13:15
  • It's simply `import pip`, not `import pip._internal`. See https://github.com/pypa/pip/issues/5373. Replacing `pip._internal` with `pip` should make this work. – miike3459 Dec 09 '18 at 13:34
  • @connectyourcharger since pip v10 it's pip._internal see: https://github.com/pypa/pip/pull/4700 – Zydnar Dec 09 '18 at 15:02
  • @Zydnar Depending on OP's version of pip, it may vary. – miike3459 Dec 09 '18 at 15:05
  • I entered your first piece of code and this is the output I get: ['pip', 'setuptools'] You are using pip version 6.0.8, however version 18.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. Weirdly enough though, when I try to update pip, through command promt, it states that I already have the latest version installed (Requirement already up-to-date: pip in c:\users\yassi\appdata\local\continuum\anaconda3\lib\site-packages (18.1)) – Y. Ben Dec 11 '18 at 14:03
  • This says, package instalation is broken. If you have only `['pip', 'setuptools']` installed you don't even loose much time by reinstalling your python enviroment. And by reinstall I mean remove previous installation. – Zydnar Dec 11 '18 at 15:25
1

I was facing the same issue. After trying it multiple times, I came across this solution and it worked.

Basically, you need to create an interpreter which points to kivy folder('kivy_venv' folder which was created using steps similar to this installation link: https://kivy.org/doc/stable/installation/installation-windows.html#start-a-kivy-application

  1. Open Pycharm
  2. Click 'File' tab in upper left corner
  3. click on 'Settings'
  4. click on 'Project:test'
  5. click 'Python Interpreter'
  6. click on 'Settings symbol' (wheel shaped) next to 'Python Interpreter'
  7. click on 'show all'
  8. click on '+' plus symbol
  9. Set 'Location' to the folder where 'PycharmProject' folder is situated.(To find out PycharmProject folder location, click on File->Open->ProjectName ) (I would recommend you to create a new folder in PycharmProject say 'kivyProject1') In my case, Location is C:\Users\Shraddha\PycharmProjects\kivyProject1
  10. Set 'Interpreter' to the folder where kivy was installed.(If you installed kivy using steps at https://kivy.org/doc/stable/installation/installation-windows.html#start-a-kivy-application , you will have 'kivy_venv' folder. Copy the complete address of 'kivy_venv\Scripts\python.exe' and paste at 'Interpreter' ) In my case, Interpreter is C:\Users\Shraddha\AppData\Local\Programs\Python\Python37\kivy_venv\Scripts\python.exe
  11. Click on 'Edit' (a pencil shaped symbol at right side) and set name Name: Python 3.7 (kivyenv1) Keep Location as it was.In my case Location is C:\Users\Shraddha\PycharmProjects\kivyProject1\Scripts\python.exe
  12. Click OK. Then OK. Now you are out of 'Settings'.
  13. Create new python program filename.py to check if kivy is imported. For that, Click File->Create new project and ensure its location is C:\Users\Shraddha\PycharmProjects\kivyProject1\filename.py
  14. In filename.py, type import kivy and run by clicking on PLAY button. You must get logs as : succesfully imported kivy
ShraddhaH
  • 11
  • 1
1

I had the same error. Am using windows 10 in VS code and below is a solution that works:

The solution was to run this:

python -m pip install kivy==2.0.0rc1

I don't know why this works, but the website instruction appears to use an older version: python -m pip install kivy==1.11.1 which fails.

You can test by running a kivyTest.py file with one line of code (below):

import kivy
D.L
  • 4,339
  • 5
  • 22
  • 45
0

Short answer:
1) Open an Anaconda command prompt.
2) Type code, and then press enter.
3) Now open your file and run your code in "this" VScode.

More explanation:
I was facing the same problem (installed kivy by running conda install kivy -c conda-forge in an Anaconda command prompt), and I couldn't import kivy in VScode that is opened normally and not from the Anaconda environment (got the ImportError: No module named 'kivy' error), but then I opened VScode from Anaconda Navigator, and run the same code and this time I didn't get any error. Then I searched if it was possible to open VScode from Anaconda command prompt (like when you want to open an IPython notebook with jupyer notebook), because the prompt loads much faster than the navigator. And found out it is done by typing code and pressing enter.

Hope this helps you!

0

if you installed it just restart your computer. I faced same problem I search on many sites
I don't get solution than shutdown pc next day I restart it its working.

0

I had a same problem with virtualenv in VS Code. The issue was that I should select Python interpreter from bin in virtualenv.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 17 '21 at 18:11
0

I had a same error and no solution that mentioned here worked for it! It happen because of virtual environment and after activating it , everything goes well!

Spring98
  • 119
  • 2
  • 12