简体   繁体   中英

Is it possible to use psycopg2 with IronPython in a Windows Form app?

I built a Windows Form to gather information about projects. This information is then uploaded to a database.

Actually, I'm using a Python script to do that (the required information is therefore hardcoded in the script by the user).

I would like to use my WindowsForm to set the variable for my script. I'm getting an IronPython runtime exception : cannot import _psycopg from psycopg2

Here's the code that set up the variables and run the script:

 private void btn_uploaddata_Click(object sender, EventArgs e)
        {

            var engine = Python.CreateEngine();
            var scope = engine.CreateScope();
            engine.SetSearchPaths(new Collection<string>(new[]
            {
                @"C:\Python27_XY",
                @"C:\Python27_XY\DLLs",
                @"C:\Python27_XY\Lib",
                @"C:\Python27_XY\Lib\site-packages",
            }));

            scope.SetVariable("project_num", tb_projnum.Text);
            scope.SetVariable("project_name", tb_projname.Text);
            scope.SetVariable("proj_client", tb_projclient.Text);

            engine.ExecuteFile(@"path/to/script.py", scope);
        }

Is it only possible to use psycopg2 with IronPython ?

The psycopg2 module uses a CPython extension and will therefore not work with IronPython. There is an experimental ctypes version which is no longer supported by the author.

In any case, you need to install the according package. Did you actually try this?

ipy.exe -X:Frames -m pip psycopg2

If pip is not found you have to ensure pip first.

ipy.exe -X:Frames -m ensurepip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM