How to configure Python Kivy for PyCharm on Windows? - python

I'm having trouble getting Kivy to work with PyCharm on Windows 7. I've managed to add most of the external libraries through File > Settings > Python interpreters > Paths Tab.
I'm using the Kivy version of Python.
When I run a Kivy app that works fine with using the [right click > send to > kivy.bat] method in PyCharm, it gives me this error:
Failed modules
Python 2.7.3 (C:/Python27/python.exe)
_imagingtk
dde
gtk._gtk
pangocairo
Generation of skeletons for the modules above will be tried again when the modules are updated or a new version of generator is available
I think that the problem might be something to do with cython, as my file fails to recognise the kivy.properties file, which is of the Cython *.pxd format.

Install and open PyCharm
If you already had it installed and have a project open, click File -> Settings (Ctrl + Alt + S). (If not, create a new project, and click the '...' (or ) next to interpreter, and skip step 2)
Under Project Settings, click Project Interpreter -> Python Interpreters
Click the little green + and select local (You can also set up an interpreter to your installed python from this list)
Point it to ..\Kivy\Python\python.exe and click ok (my path was c:\Program files (x86)\Kivy\Python\python.exe since that is where I unzipped the kivy zip file to)
I have also attached a settings.jar file. This is the kv language definition. It is not complete, but it helps a lot.
Click File->Import and select the settings.jar file.
Only FileTypes will be ticked. Import this and you will have "kv language file" definition under File->Settings-IDE Settings->File Types
Open a kv file to see the differentiation in colours, as well as autocomplete
Widgets are type 1
Properties are type 2
all events (on_something) are type 3
type 4 is just self and root.
That is all for PyCharm, the rest is Windows 7 specific
open a command prompt and browse to your ..\Kivy\Python\lib folder
type mklink /D kivy "..\Kivy\kivy\kivy" (my line was mklink /D kivy "c:\Program files (x86)\Kivy\kivy\kivy")
This will set up a symlink so that your all your kivy python files are read and their definitions are included, in order to get autocomplete
Now we need to set up the environment variables. You could do this per project inside PyCharm, but might as well do it in windows, so you only need to select the python interpreter each time.
Click start and type envir Select the second one. (System variables) (You could also get here with Win+PauseBreak-> Click Advanced system settings)
Click Environment variables
Now add these (once again, just point to wherever you have your kivy folder. You can also find all these in the kivy.bat file, just find and replace the variables with your path)
GST_PLUGIN_PATH
c:\Program Files (x86)\Kivy\gstreamer\lib\gstreamer-0.10
GST_REGISTRY
c:\Program Files (x86)\Kivy\gstreamer\registry.bin
PATH
c:\Program Files (x86)\Kivy;c:\Program Files (x86)\Kivy\Python;c:\Program Files (x86)\Kivy\gstreamer\bin;c:\Program Files (x86)\Kivy\MinGW\bin;c:\Program Files (x86)\Kivy\kivy;%PATH
Restart your machine. (For the environment variables to load)
Now when you open your kivy project, just select the Kivy interpreter you set up earlier, and bobs your uncle.

This Kivy's Wiki page Setting Up Kivy with various popular IDE's has a better answer and detail commands. It is copied below with added information for Pycharm 3.4.
Go to your unzipped Kivy folder, create a symbol link for "kivy.bat" pointing to "python.bat" in the same directory (mklink python.bat kivy.bat).
Add 2 new Python interpreters in PyCharm.
Bootstrapper: Choose the earlier created "python.bat" from the Kivy package folder.
Project Interpreter: Choose the "python.exe" from the Python subdirectory in the Kivy package folder.
For the project interpreter, add a path to the "kivy" directory directly contained in the Kivy package folder. In PyCharm 3.4, the path tab is hidden in a sub menu. In Project Interpreter, click the tool icon next to the interpreter dropdown list, click more... (the last one), in the list of all project interpreters, select Run-Configuration Interpreter, on the right side there are five icons (+, -, edit, virtual env, and path), click path to add the Kivy sub-directory in unzipped Kivy folder.
Save all settings and ignore warnings about "Invalid output format". Make sure that the project interpreter is set to our earlier created configuration.
Create a new run configuration and set the Python interpreter to our earlier created bootstrapper.
Simply run the configuration to execute your Kivy application

Alternatively simply:
Unpack the Kivy package into a folder.
Create a symbolic link for "kivy.bat" pointing to "python.bat".
Add 2 new Python interpreters in PyCharm. One will be used as the
project interpreter, the other as the run-configuration interpreter.
Project Interpreter: Choose the "python.exe" from the Python subdirectory in the Kivy package folder.
Run-Configuration Interpreter: Choose the earlier created "python.bat" from the Kivy package folder.
For the run-configuration interpreter click on the paths-tab and add the path to the kivy directory directly contained in the Kivy package folder.
Save all settings and ignore warnings about "Invalid output format".
Make sure that the project interpreter is set to our earlier created configuration.
Create a new run/debug configuration and set the Python interpreter to our earlier created run-configuration interpreter.
Simply run the configuration to execute your script
By having two seperate configurations it's possible to use the "kivy.bat" or "python.bat" file to bootstrap Kivy and run the target-script and use the other configuration with the added path for proper code-completion. If PyCharm tries to use the "kivy.bat" or "python.bat" interpreter to build the module skeletons it fails/hangs. Some screenshots of the configuration:

KV Lang Auto-completion and Highlighting
You just need to import the .jar file. Works fine on linux.

KV Lang Auto-completion and Highlighting -> Link
Description
KV Lang Auto-completion and Highlighting
Xuton has kindly developed a file type extension that gives you full syntax highlighting and auto-completion for KV files.
To install:
Download this file .
On Pycharm’s main menu, click File -> Import (or Import Settings).
Select the jar file you just downloaded and PyCharm will present a dialog with filetypes ticked. Click OK.
Restart PyCharm for the changes to take effect.

A very simple version which worked for me was:
Follow the instructions in Setting-Up-Kivy-with-various-popular-IDE's
Install the packages in the Project Interpreter
Go to: [File]-[Settings]-[Project: ProjectName]-[Project Interpreter]
Install: Pillow, image, kivy.deps.glew, kipy.deps.gstreamer, kivy.deps.sdl2, pygame, kivy
At least for me this solved my issue with the PIL module import error (The module PIL is called now Pillow) and also with the sdl2 ddl import error.
Update
Actually, I just installed the modules from the second step in PyCharm on a different PC without completing the first step and Kivy was working anyway.

if you do not installed kivy go to Kivy İnstallion,
Go to your python directory (e.g: C:\Python),
Open Lib folder (e.g: C:\Python\Lib),
find site-packages and open it,
Copy kivy folder to Python\Lib.
That's it. Enjoy Kivy :)

Related

python interpreter not found for Django project after re-installing python

I have made an application using Django in pycharmIDE.after completing my project I deleted python from my machine and re-installed now when I run my application it shows
No Python at 'C:\Users\Raisan\AppData\Local\Programs\Python\Python37-32\python.exe'
how can I set python interpreter in Pycharm
In order to set Python interpreter in a PyCharm project, you should click on:
File -> Settings -> Project -> Project Interpreter
Then, on the top of the window, you should have a dropdown selection with Project Interpreter label. Here you can choose the project interpreter for your project.
If the interpreter is not present, click on the settings icon (gear wheel) on the right anc choose "Add".
Then choose "Existing enviroment" and navigate througth your directories until you will find your Interpreter.
Remember that you have to select the python.exe file.
Please set environment variable and try it otherwise uninstall python and and reinstall it but that time select custom python install and choose appropriate options like IDEdevelopment environment pip etc.

Pycharm storing packages in separate files?

I am using Python 3.7 on windows. I installed Pycharm and successfully wrote a script which I am now trying to schedule using Windows Task Scheduler, which comes with its on set of complications. One thing I have noticed about Pycharm is I think it has created a separate file directory to store any packages I add to a script (maybe in something called "venv"? Instead of using the User/Python37/Scripts file.
This means when I try to run my script in the command prompt, python.exe looks for packages and cannot find them. Also if I go into my Pycharm project folder is see another instance of a Python Application file different than the Python Application stored in User/Python37. I think this also creates problems but I am not 100% sure.
I am hoping someone has seen this issue and can help me align where Pycharm stores packages. Any help would be greatly appreciated.
You can also simply add your script/package into your python path.
For that follow this awser : How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
PyCharm creates a virtual environment (venv) where you can keep the python version and the libraries used in a specific project.
You can add libraries to the specific environment through the Pycharm GUI:
File > Settings > Project: Patterns > Project Interpreter > Install (green +)
Find your package and click Install Package in your venv.
You can see all the installed packages and their version in the path:
File > Settings > Project: Patterns > Project Interpreter
You can also use pip install, if you want to go through CLI, but be sure to use the virtual environment's pip (located in project_folder/venv/Scripts).
If for some reason you want to use the python version outside the virtual environment, go to the following path in PyCharm:
File > Settings > Project: Patterns > Project Interpreter
In the Project interpeter dropdown menu, you should find other python's location; choose the one you prefear. If you don't see your standard python version (usually in C:\python\python.exe, or something similar), you can add it by clicking on the settings menu, and specify the path to the desired python version in Base interpreter:.
In this window, you can find other settings to configure the interpreter as you want.

How can I run my Python program like a normal program from the desktop?

I'm creating a program that I would like to use as a normal program as well as continue to code it on the side. To do this I first tried creating a shortcut of the .py file in my PyCharms project folder and sent it to desktop. When I double-clicked the shortcut the command prompt would open for a second and then shut. It's a PyQt4 program so I'm not sure if this has any bearing. The program has been coded in Python 3.4. I've noticed that when I open the command prompt and type 'python' it shows Python 3.5 for some reason so I'm not sure if this has any bearing on the situation.
If you've ever programmed in C# I'd like to be able to build a solution and then rebuild the solution when I've updated the code so that I can access the program as a normal program as well as continue to improve the code of it.
Thanks for any help.
Managed it thanks to the link above. Uninstalled Python 3.5 and set my PATH variable to C:\Python34. Downloaded pyinstaller and installed it using PIP. Then navigated to Python34/Scripts and dragged myFile.py (the one to be made an .exe) into it. Ran pyinstaller.exe --windowed myFile.py to create the exe which then went to my dist folder. Created a shortcut and it worked perfectly.
Go to your environmental variables (Right click on Computer > Properties > Advanced system settings > Environment Variables...). Find Path in System variables, select it, and click edit. Remove the Python 3.5 path and replace it with your python 3.4 or virtual environment folder that has python.exe in it.
Make a shortcut on your desktop that points to the .py file that you are editing.
If you have all of the dependencies right then double clicking the .py file's shortcut should run your program.
Other wise you can pip install cx_freeze and use cx_freeze like setuptools. Create a setup.py file and build the executable.
If you want to install this executable I suggest using Inno Setup. It is pretty straight forward on how to use and has an easy wizard that helps you build a basic installer.

Using IntellijIdea within an existing virtualenv

I want to use Intellij Idea within my existing python django project which was built within a virtual environment in ubuntu. How do I configure Intellij Idea to use the libraries of the virtual environment?
For PyCharm 2018
As per documentation from Pycharm:
In the Project Interpreter page, click and select Add.
In the left-hand pane of the Add Python Interpreter dialog box, select Virtualenv Environment. The following actions depend on whether
the virtual environment existed before.
If Existing environment is selected:
Specify the required interpreter: use the drop-down list, or click Select an interpreter and find one in your file system.
Select the check-box Make available to all projects, if needed.
Click OK to complete the task.
For Pycharm 2016 and later
To add an existing virtual environment to the list of available interpreters
In the Project Interpreter page, click .
In the drop-down list, choose Add local.
In the Select Python Interpreter dialog box that opens, choose the desired Python executable, located inside the virtual environment folder, and click OK.
Go to this link for more information.
For Older versions:
Well I solved the above problem. I have added virtualenv folder's python to project sdk. The virtualenv directory's python( for example venv/bin/python2.7) needs to be added to Intellij Idea project path. Example: need to go to file>project structure (intellij Idea)
press new in Project SDK, and add new path to virtualenv's python directory like this:
Go to Modules>Dependencies and set your module sdk to Python SDK which is marked on this picture:
Click on Django (option marked in next the image) and set Django project root, Settings,Manage Script like this:
Now press ok and final look of the Project settings:
Now need to run the project.
The above answer is based on older version of IntelliJ. For new, look here: https://www.jetbrains.com/help/pycharm/2016.1/adding-existing-virtual-environment.html

IntelliJ Python plugin & Run classpath

I have a project located at /home/myself/workspace/Project1, for which I created an SDK from a Python 2.7.3 Virtualenv I have setup.
This project uses some external code that I have in an accessible directory, e.g. /home/myself/LIBRARY; this directory contains several directories with code, docs etc....
For example, there is a module "important_util" located at /home/myself/LIBRARY/mymodule/important_util.py.
Now, I added the whole dir /home/myself/LIBRARY in the SDK Classpath, and in the Editor window it appears just fine. The imports and calls are recognized and I can also navigate through the code in LIBRARY directories.
The problem is that, when I try to run my program, it fails at the first import using LIBRARY!!!
Traceback (most recent call last):
File "/home/myself/workspace/Project1/my_program.py", line 10, in <module>
from mymodule import important_util as ut
ImportError: No module named mymodule
I also tried to add the same directories to the section "Global Libraries" in the Sources section...but no luck.
I can't seem to find a way to add this code to the Run classpath, how would I be able to do this?
Make sure you have __init__.py in mymodule directory:
The __init__.py files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as string, from unintentionally
hiding valid modules that occur later on the module search path. In
the simplest case, __init__.py can just be an empty file, but it can
also execute initialization code for the package or set the __all__
variable, described later. ©
UPDATE: In IntelliJ IDEA additional directories should be added as Module Dependencies or configured as Libraries (to be added to the Dependencies) instead of the Classpath tab of the Python SDK:
I've verified that this folder (D:\dev\lib) is added to the PYTHONPATH and import works.
In IntelliJ 14 it's a little different, you are modules/eggs like so:
Go to File -> Project Structure
Now select Modules and then "Dependencies" tab
Click the "+" icon and select "Library"
Click "New Library" and select Java (I know it's weird...)
Now choose multiple modules / egg and "OK".
Select "Classes" from categories.
Give your new library a name, "My Python not Java Library"
And finally click "Add Selected"
From Version of 2017.1 adding it has been changed again. There is no project structure in the file menu. Writing current procedure down:
Go To Preference/Settings. File -> Settings (IDE Name -> Preferences for macOS)
Select Build, Execution, Deployment
Select Python Interpreter
Select on drop-down menu of project interpreter and select the path of path of version of Python required for the project.
Click on Apply and wait for few minutes to let IntelliJ index the python packages.
All error should be gone now and You should be able to see Python used in the project in the list of external libraries.
Happy Coding.

Categories

Resources