Python programming on Eclipse with Pydev - python

I need major help getting started! I managed to create a new project, and add python.exe as the interpreter. But when the project is created it's blank. How do I start programming? Ugh.

Create PyDev project
Add "Source Folder" under the project
Add "Modules" to the "Source Folder"
Get coding :-)

Open a new text file and start writing code?

You need to make sure you create a Pydev project. If you don't already have a Python interpreter installed, you'll need to get one (this is mainly a problem on Windows, ActiveState is a great commercial grade distribution, python.org also has some great free builds of Python.) Once this is complete, you'll need to get into your global Pydev preferences (not the preferences for your specific project, but for all Pydev projects) and select the interpreter of your choice. Pydev will automatically scan the libraries included with your interpreter distribution and make them available for code completion. Then, you'll need to create a source file in the 'src' folder that contains code. Run it by choosing a run configuration that matches up with your interpreter choice (regular old Python in your case, not Iron python or Jpython). Most entry point python modules will have a main function like in C/C++ that looks like this: What does if __name__ == "__main__": do?

Related

Moving PyCharm projects folder and keeping reference to python interpreter

I'm using macOS 10.15.4 and PyCharm 2019.3.4
I currently have a folder inside the PyCharmProjects folder (that is automatically created when PyCharm first runs) where I keep projects for a class. I want to move this folder, which contains multiple other folders which each contain PyCharm projects, somewhere else on my computer (like the Desktop). The problem is if I move the folder (or even just a single PyCharm project) the next time I open the project in PyCharm, it says "Invalid python interpreter selected for the project." Now I can manually go into PyCharm preferences and point it to the new location I moved the folder so it can use the correct python interpreter. But this would be tedious to do for every single project I have. (And yes, every project I have uses its own interpreter and virtual environment.)
Is then a way I can move a folder containing multiple PyCharm projects without loosing the references to each of their respective python interpreters?
(Feel free to reword this question or the title.)
PyCharm uses configurations from your home directory. The docs say
macOS
Configuration
~/Library/Preferences/<PRODUCT><VERSION>
Caches
~/Library/Caches/<PRODUCT><VERSION>
Plugins
~/Library/Application Support/<PRODUCT><VERSION>
Logs
~/Library/Logs/<PRODUCT><VERSION>
where <PRODUCT> is PyCharm.
They are xml files. Among them also a list of configured interpreters. They are separate because they are available for all your projects to be chosen as the default interpreter. You'll have to edit the xml files.
For some reason PyCharm does not help with a moved virtualenv directory directly. (see comment from engineer: https://youtrack.jetbrains.com/issue/PY-32435#focus=Comments-27-3139072.0-0)
On the upside, everything else seems to work, except the path to the venv python. That one can be changed through preferences/project preferences/python interpreter. Just click on "add", "existing", and select the python compiler in your project folder.
Try not to select the ".virtualenvs" python compiler, as if you fumble like I did, it seems it's a bit of a pain to get rid of it and go back to the right one. If you do that, renaming the project folder once more gets you back to square one and you get to try again.
Note that I'm using MacOS. YMMV.

How to setup Python IDE make auto complete work?

I want to write IDAPython scripts and plugins. The problem is to get my Python IDE (PyCharm) to resolve the imports correctly and therefore get code completion correct.
Here is an example. I want that code completion works for IDAPython commands like:
ea = ScreenEA()
or
ea = idc.get_screen_ea()
Here is what I did. I downloaded IDAPython and put the source in my Python projects source folder.
I also copied the python folder from the IDA install directory into my Python projects source directory.
It turns out, that Pycharm cannot resolve the functions correctly.
How to setup Python IDE make auto complete work?
As long as the IDAPython files show up in you Pycharm project, or are in the same folder, you simply need to import the file you want to use.
So for your idc.get_screen_ea() example, as long as you have this at the top of your python file you are working on:
#import idc
Then you will get suggestions for anything involving idc.
Here is the method I took for PyCharm Windows version. First create an venv based project with the python version match your IDA python version.
File -> Settings -> Project -> Python Interpreter
Click setting icon, select Show All...
Select the interpreter you currently use, click Show paths for the selected interpreter
At the popup window, add your %IDA_INSTALL_PATH%\python
Then you can use PyCharm IDE features to write IDA python plugin.

How to configure PyCharm to develop LibreOffice Python macros?

I've installed Python 3.5.1 as default in Win7(x64) for all my projects in Python.
I use PyCharm 5.0.5 community edition for develop Python scripts and its default settings has "Default Project Interpreter" as "3.5.1 (C:\Python35\python.exe)"
At my work we are migrating from MS Office 2007/2010 to LibreOffice-5. I wrote some macros in VBA, despite of I'm not a VB enthusiastic. Basic lacks good data structures, such as lists (I love list comprehensions), dictionaries, sets and tuples. So, I want to rewrite the VBA macros in LibreOffice-5 Python script macros.
LibreOffice-5 installation has its own embebed Python at "C:\Program Files (x86)\LibreOffice 5\program", version 3.3.5. Scripts of Python in LibreOffice-5 installation is at :
Libre Office Macros; "C:\Program Files (x86)\LibreOffice 5\share\Scripts\python"
My Macros; "C:\Users\trimax\AppData\Roaming\LibreOffice\4\user\Scripts\python"
The question is simple:
I need configure PyCharm settings to develop the python scripts of LibreOffice macros with the embebed python version. I don't know if I need to set a virtual environment or if I can just to set the Project Interpreter.
By the way, are there any method to insert macros in the document, to share it with the document, as the VBA Project Modules?
From the PyCharm documentation, it sounds like you could use a virtual environment to target LibreOffice (likely Python 3) and OpenOffice (likely Python 2) in two different projects. Otherwise it looks like a local interpreter is enough.
To test PyCharm, I did the following:
Download PyCharm and create a new project.
It asks which interpreter to use. Click on the gear icon and specify Add Local. Browse to C:\Program Files (x86)\LibreOffice 5\program\python.exe.
Create a new python file.
Then add this code:
import uno
from com.sun.star.awt import Point
p = Point(2,3)
print(p.X)
points = uno.Any("[]com.sun.star.awt.Point", (p,))
print(repr(points))
It underlined the com import statement, although it's not actually an error. PyCharm did recognize the other statements such as uno.Any.
To run, go to Run -> Run. It ran successfully and printed results as expected.
Instead of an IDE, I typically just use a text editor. From what I have seen, a lot of the IDE tools (syntax highlighting, auto completion, debugging) do not work very well with UNO anyway. It is better with Java, but that is a different topic.
By the way, are there any method to insert macros in the document, to share it with the document [...]?
To embed Python code into a document, unzip the .odt file and follow the instructions here.
To embed Python code into a documents, I recommend using the APSO extension. You can download the LibreOffice extension here. It creates a new menu item: Tools...Macros...Organize Python Scripts. From there you can embed or export files.

Auto-Completion for Panda3d in PyCharm

Does auto-completion for the Panda3d library working with PyCharm? It seems PyCharm cannot automatically create the Python skeletons for this library. I would also be happy if I could at least manually define those stubs in PyCharm.
Any ideas how to tell PyCharm what Python modules and classes are there in a "binary" library?
For me it worked just selecting in Settings > Project Interpreter the panda python interpreter (python.exe, not ppython.exe.).
If you wanna use ppython.exe you have to rename to something starting with "python" like pythonpanda.exe, since Pycharm only considers an interpreter something that starts with "python". Anyway, ppython and python are supposed to be the same.
EDIT
Another thing that can make it work, is using another python interpreter (the standard, virtualenv, whatever) and placing a path file in a folder within the PYTHONPATH.
In other words:
Create a text file named panda3d.pth
Write two lines
path\to\pandafolder
path\to\pandafolder\bin
Save it in the site-packages of your python interpreter
Configure Pycharm to use this interpreter

eclipse: Run/Debug current file

In search of a Python debugger I stumbled upon Aptana, which is based on eclipse.
Often, I want to debug a single python script. However, Aptana won't let me run/debug the currently opened file directly.
Instead, it requires me to create a debug/run configuration for each file I would like to run/debug. Alternatively I could create a Python project in Aptana.
But: I don't want to. I just want to be able to run or debug the currently opened file. This way I would like to debug my scripts without being forced to create a project first (for each single script!).
Can it be that hard?
This is because Aptana/Eclipse doesn't "realize" that the file you opened should be debugged using the Python debugger as it's not associated with a Python project/perspective (there's a lot of environment setup when a project is created in Aptana/Eclipse).
The simplest solution, IMO, would be to create a simple sandbox Python project and just stick your files in there to run/debug. Aptana should then realize you're dealing with Python and start running the Python debugger without setup (that's my experience w/ PyDev in Eclipse, at any rate).
See: Run External Python Programs with Eclipse PyDev (note: you really need a project, but your file doesn't need to be in it).

Categories