How do I get kivy installed in a Pycharm Virtualen

2019-08-18 10:59发布

问题:

This question already has an answer here:

  • How to configure Python Kivy for PyCharm on Windows? 6 answers
  • Pycharm running Kivy applications 1 answer
  • No module named kivy in PyCharm 2 answers

I am running Pycharm under Ubuntu and am trying to get Kivy installed in my project (virtualenv). I followed the Kivy instructions but those install it in the default environment. So, at my terminal prompt when I go into python I am able to successfully "import kivy". However, when I am in a Pycharm project with virtualenv, I get an error when I try to import from the Python console:

import kivy
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/mnt/bigdisk/apps/pycharm-2017.3.4/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 20, in do_import
    module = self._system_import(name, *args, **kwargs)
ImportError: No module named kivy

I've even tried activating the virtualenv in my terminal and then performing the installation instructions. When I do this, I am told it is already installed. I think this is happening because to install kivy you have to make a sudo call (sudo apt-get install python-kivy) which is resetting the environment. I even tried sudoing to bash activating the environment and installing python-kivy, but again, it said it was installed. How do I get Kivy installed in my Pycharm virtual environment?

回答1:

Kivy Virtual Environment Installation using PyCharm

Assumption

Assuming virtualenv or/and virtualenvwrapper already installed.

sudo pip install virtualenv virtualenvwrapper

Launch PyCharm

  1. Click Congfigure
  2. Click Settings
  3. Click project Interpreter
  4. Click cogwheel next to the Project Interpreter field, and choose the option Add...
  5. New environment is the default selection.
  6. Type kivyinstall for the Location e.g. /home/iam/.virtualenvs/kivyinstall
  7. Select Base interpreter e.g. Python 3.5, or Python 2.7 from the drop down list.
  8. Do not select the Inherit global site-packages check-box.
  9. Select the Make available to all projects check-box, if needed.
  10. Click OK button
  11. After pip and setuptools installed, click + beside the column heading, Latest.
  12. Type cython in the search text input box under Available Packages Dialog.
  13. Select Specify version check-box to load version 0.25.2 which is compatible to Kivy version 1.10.0
  14. Click Install Package button.
  15. After cython 0.25.2 version installed successfully, type kivy in the search text input box under Available Packages Dialog.
  16. Click Install Package button.
  17. After kivy installed successfully, create a new project.
  18. In your opened new project, if kivyinstall did not appear in the expanded External Libraries then click File » Settings... » Project Interpreter and select Python...(kivyinstall) e.g. Python 3.5 (kivyinstall)

Print Screens



回答2:

apt-get install python-kivy installs kivy for a system interpreter and not inside a virtualenv no matter what sudo tricks you did. There's a special paragraph in the official docs for installing kivy inside a virtualenv: https://kivy.org/docs/installation/installation-linux.html#installation-in-a-virtual-environment