Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i´am trying run a python code through a call in c#, but I need to import some packages and when I run the code the following message is displayed: Missing required dependencies ['numpy', 'pytz']'.

I need these packages: numpy, pandas, collections and sklearn

This is my C# code:
C#
private void button1_Click(object sender, EventArgs e)
{
    Thread myThread = new Thread(new ThreadStart(startPy));
    myThread.Start();
}

public static void startPy()
{
    string filename = "Scripts\\Program.py";
    string path = Assembly.GetExecutingAssembly().Location;
    string rootDir = Directory.GetParent(path).FullName;

    RunPythonFile(rootDir, filename);
}

public static int RunPythonFile(string rootDir, string filename)
{
    ScriptEngine engine = Python.CreateEngine();

    ScriptSource source;
    source = engine.CreateScriptSourceFromFile(rootDir + "\\" + filename);

    ScriptScope scope = engine.CreateScope();

    int result = source.ExecuteProgram();
    return result;
}

I have a button and this button call a thread that call my Python code which is in a folder called Script in the "bin / debug" path of the visual studio project

This is a part of my Python Code:
Python
<pre>import clr
import sys
import time
clr.AddReference("System")
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReference('IronPython')

from System.Windows.Forms import Application, Form, Button, Panel
from System.Drawing import Size

from IronPython.Compiler import CallTarget0

sys.path.append("C:\\Users\\Matheush\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs")
sys.path.append("C:\\Users\\Matheush\\AppData\\Local\\Programs\\Python\\Python37-32")
sys.path.append("C:\\Users\\Matheush\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\site-packages")

import pandas as pd
from collections import Counter
import numpy as np

When it arrives in the part of the imports it presents that error message: <Missing required dependencies ['numpy', 'pytz']'

How do I import these dependencies into visual studio?
Can someone help me please? Thank you

What I have tried:

I already tried to install the numpy and the other packages again, but it did not work. I tried to use the python distribution of anaconda3, but it also did not work.

In the PyCharm IDE of python the code works, but when I try to execute it by calling it from C # it presents this error in dependencies
Posted
Updated 18-Jan-19 3:08am

1 solution

You need to set the PYTHONPATH environment variable[^] to the location of the external libraries.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900