error CS0234 name does not exist in the namespace?

patrick

Well-known member
Joined
Dec 5, 2021
Messages
269
Programming Experience
1-3
Hello


What should I do about error CS0234?


C#:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Management;

// IronPython
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Modules;
using System.Xml.XPath;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var engine = IronPython.Hosting.Python.CreateEngine();
            var scope = engine.CreateScope();
            var paths = engine.GetSearchPaths();

            paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310");
            paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\DLLs");
            paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib");
            paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages");
            paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages\pandas");
            paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages\numpy");
            paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages\matplotlib");
            paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages\statsmodels");

            engine.SetSearchPaths(paths);

            var source = engine.CreateScriptSourceFromFile(@"C:\Users\abc\Untitled-1.py");
            source.Execute(scope);

            var getPythonFuncResult = scope.GetVariable<Func<string>>("getPythonFunc");

            var sum = scope.GetVariable<Func<int, int, int>>("sum");
            Console.WriteLine(sum(1, 2));

            var test_arry = scope.GetVariable<Func<byte[], IronPython.Runtime.List>>("test_arry");
            IronPython.Runtime.List list = new IronPython.Runtime.List();
            list = test_arry(new byte[4] { 0, 1, 2, 3 });

        }
    }
}



error CS0234: The <List> type or namespace name does not exist in the namespace



20240512.png
 
I am assuming that you had installed IronPython 3.4.1. It looks like the List class has been renamed to PythonList.

Or you could go back to IronPython 2.7.12.
 
I am assuming that you had installed IronPython 3.4.1. It looks like the List class has been renamed to PythonList.

Or you could go back to IronPython 2.7.12.


you are smart !
What should I do about this error?

This is IronPython 3.4.1 AND Python3.10

20240512.png




This is the corresponding Python source that links C# and Python.
Use [pandas] and [numpy] in Python Source.
My guess is that [pandas] and [numpy] are used in the Python source that is trying to integrate with C#.
This seems to be because [pandas] and [numpy] are not supported by IronPython 3.4.1


C#:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Management;

// IronPython
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
using IronPython;
using IronPython.Hosting;
using IronPython.Runtime;
using IronPython.Modules;
using System.Xml.XPath;

var engine = IronPython.Hosting.Python.CreateEngine();
var scope = engine.CreateScope();
var paths = engine.GetSearchPaths();

paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310");
paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\DLLs");
paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib");
paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages");
paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages\pandas");
paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages\numpy");
paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages\matplotlib");
paths.Add(@"C:\Users\abc\AppData\Local\Programs\Python\Python310\Lib\site-packages\statsmodels");

engine.SetSearchPaths(paths);

try
{
    var source = engine.CreateScriptSourceFromFile(@".\python\Calculator.py");
    source.Execute(scope);

    var getPythonFuncResult = scope.GetVariable<Func<string>>("getPythonFunc");

    var sum = scope.GetVariable<Func<int, int, int> > ("sum");
    Console.WriteLine(sum(1, 2));

    var test_arry = scope.GetVariable<Func<byte[], IronPython.Runtime.List>>("test_arry");
    IronPython.Runtime.List list = new IronPython.Runtime.List();
    list = test_arry(new byte[4] { 0, 1, 2, 3 });


}
catch (Exception ex)
{
  
}



C#:
# Calculator.py ==== Python3.10 Version

import numpy as np

def getPythonFunc():
    return "hello"


def sum(a, b):
    c = a + b
    return c


def test_arry(test_list):
    for i in test_list:
        print(i)

    rst_arry = []

    rst_arry.append("1");
    rst_arry.append("2");
    rst_arry.append("3");
    rst_arry.append("4");

    return rst_arry


print("hello iron python")
 
Last edited:
The class IronPython.Runtime.List from IronPython 2.7.12 is now named IronPython.Runtime.PythonList in IronPython 3.4.1.
 
The class IronPython.Runtime.List from IronPython 2.7.12 is now named IronPython.Runtime.PythonList in IronPython 3.4.1.



Added this too.
paths.Add("C:/Users/abc/AppData/Local/Programs/Python/Python312/Lib/site-packages/numpy");

Error Microsoft.Scripting.SyntaxErrorException: 'invalid syntax' occurs in
var source = engine.CreateScriptSourceFromFile(@".\python\Calculator.py");
source.Execute(scope);
 
That error has nothing to do with the PythonList.

That error has something to do with the "Calculator.py" Python script. The exception should have additional details like which line number or near which token. Any which way, that is a Python issue. This is a C# forum. You don't go to your podiatrist asking about your toothache.
 
Back
Top Bottom