When implementing a Visual Studio extension to be used with Python files in the VSCode editor, how can one programmatically get the path of currently selected Python environment?
My guess is that it is done with python.pythonPath, but I can't seem to make it work. If I use that directly within an extension js, it crashes. I also tried:
let python = vscode.extensions.getExtension('python');
python.pythonPath
python.pythonPath is a setting, so you can do this with VSCode's generic settings API (instead of having to rely on the Python extension exposing an API specifically for this):
vscode.workspace.getConfiguration("python").get("pythonPath")
Related
My application has built-in Python interpreter and I need to debug Python code there. However Visual Studio Code allows to choose only from interpreters found in paths and named python/python2/python3.
I tried to set python.pythonPath in settings.json to point to my application, but Visual Studio Code doesn't recognize it as valid Python interpreter.
Sure, I need to make sure that my application behave like Python interpreter and pass all command-line parameters to Python ptvsd_launcher.py.
The "python.pythonPath" setting is how you can specify a Python interpreter that is not automatically detected. If the binary does not function as a normal python binary then unfortunately there's no way for the extension to use it.
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.
I found the configuration for cpp (https://github.com/Microsoft/vscode-cpptools/blob/master/Documentation/Debugger/gdb/Windows%20Subsystem%20for%20Linux.md) and tried to change it for python debugging but it doesn't work. Any suggestion to make it work?
It should be mentioned that the Python extension for VS Code does not officially support WSL yet, but the enhancement request has been made and we do plan on supporting it.
Beyond extensions installations, IDE_PROJECT_ROOTS environment variable also may affect the debugger. For usual WSL standalone python code debugging, making sure this variable is not set (or set to the location of the files) when the VS code is opened helps.
For "step into" debugging of jupyter notebook having the python files path(s) as a part of IDE_PROJECT_ROOTS (for example export IDE_PROJECT_ROOTS="/tmp:/foo_pythonfilespath" set in .bashrc) will help to carry out "step into" python-code debugging in VSCode.
This is now supported and just requires installing the Microsoft Python extension and then to quote the documentation on remote debugging with WSL:
Once you've opened a folder in WSL, you can use VS Code's debugger in
the same way you would when running the application locally. For
example, if you select a launch configuration in launch.json and start
debugging (F5), the application will start on remote host and attach
the debugger to it.
See the debugging documentation for details on configuring VS Code's
debugging features in .vscode/launch.json
I run Python program which uses couple of source paths on runtime.
I put these rows on my /.bashrc file:
source home/raphael/kaldi/aspire/s5/cmd.sh
source home/raphael/kaldi/aspire/s5/path.sh
So when I'm running from terminal everything works fine and Python manage to locate paths.
However when I'm trying to run through PyCharm for DEBUG purposes mostly it seems that PyCharm can't locate the paths.
Is there anyway to add the paths manually for PyCharm or make it read /.bashrc file. What I am missing?
You can try using the options available in the Run/Debug Configuration settings (Run > Edit Configurations...)
You can set environment variables individually (such as $PATH), or at the bottom is a section to define external tools (scripts) to be run when your Python code is run or debugged. From that sub-section, you could set your bash scripts to be run each time you start debugging.
Alternatively, see if using os.environ would work for your project. Check the docs for more information.
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.