Using Python to Launch and Use IDEA

9 posts / 0 new
Last post
username12654
Offline
Joined: 06/24/2019 - 13:53
Using Python to Launch and Use IDEA

Hi, is it possible to use IDEA from Python without first opening IDEA? Is there some code I can add to be able to do this? 
 
The code below works if I open IDEA before running the code. This is the error I get when running the below code: "Invalid database or the database hasn't been specified yet."
Thank you.
 

import win32com.client as win32ComClient
 
if __name__ == "__main__":
 
try:
 
resultDbName = "Sample-Employees-Extraction.IMD"
criteria = 'COUNTRY=="Mexico"'
 
idea = win32ComClient.Dispatch(dispatch="Idea.IdeaClient")
table = idea.OpenDatabase("Sample-Employees.IMD")
 
task = table.Extraction()
task.IncludeAllFields()
task.AddExtraction(resultDbName, "", criteria)
task.PerformTask(1, table.Count)
 
idea.OpenDatabase(resultDbName)
 
finally:
 
task = None
table = None
idea = None
 

klmi
Offline
Joined: 02/13/2019 - 08:41

Yes it is possible to run Python code without opening IDEA. The only line I can see that seems not working is criteria = "COUNTRY=="Mexico"". You have to specify criteria = "COUNTRY==\"Mexico\"". If that doesn't help try run your script line by line.

username12654
Offline
Joined: 06/24/2019 - 13:53

Thank you for the reply, I ended up using single quotes around the criteria and the line worked (I updated the original post as well). I am getting the "Invalid database or the database hasn't been specified yet." error when I try set my table variable to my database.

table = idea.OpenDatabase("Sample-Employees.IMD")

I made sure 'Sample-Employees.IMD' is available so I'm not sure what's the issue.

The script works perfectly if I first open IDEA. I'm wondering if I'm missing a step to initialize IDEA through Python.

klmi
Offline
Joined: 02/13/2019 - 08:41

There have been double quotes in the original post (I copied that line). Your problem seems to depend on the project path. As long as I tested IDEA always opens the last used project folder. For files in that folder only the filename is necessary. So, finally the best solution would be to use absolute paths to avoid such errors. 

username12654
Offline
Joined: 06/24/2019 - 13:53

Yes, my last used project folder is always opened and it contains the specified database file ("Sample-Employees.IMD"). I even tried to use the absolute path and it didn't unfortunately work. Thanks for the help though.

klmi
Offline
Joined: 02/13/2019 - 08:41

If you open IDEA first and run the code from the library IDEA's integrated Python version is used. Without starting IDEA first, you could try whether it works to run the code from PythonWin (http://ideascripting.com/forum/pythonwin-ide-and-gui-framework). That's the same Python version as delivered with IDEA. I've run my tests on it because I have no further Python interpreter installed on my system and it worked.

username12654
Offline
Joined: 06/24/2019 - 13:53

I'm not sure but it seems to be something to do with how quickly the script is executed. I am able to run the script line by line and it works, but if I just run the script (even with PythonWin) I get the "Invalid database or database hasn't been specified yet" error.

username12654
Offline
Joined: 06/24/2019 - 13:53

I got the script to work by importing the time module and using the time.sleep() function to hault the code for  2.5 seconds after the "idea = win32ComClient.Dispatch(dispatch='Idea.IdeaClient')" line.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

That is interesting, sorry I have been busy on other projects and haven't had time to look into this.  Thanks for sharing your solution.