C#-Pandas Process Error (.NET): no module named pandas - python

I have a simple GUI with two buttons and a rich text box. Button One uses the OpenFileDialog() class to fetch a video file and write the path as a string in the rich text box. Button 2 takes the value of the rich text box and sends it file to a Python script. This part works fine. Here is the function for sending the file.
I am operating out of Visual Studio 19.
C# .NET framework
public void PotatoOne(string path)
{
var neat = "r'" + path;
var shell = "python.exe";
Process x = new Process();
x.StartInfo = new ProcessStartInfo()
{
FileName = shell,
Arguments = ($"\"{"C:\\Users\\BeckerLab\\source\\repos\\Final Implementation\\Final Implementation\\untitled7.py"}\" \"{neat}\""),
ErrorDialog = true,
WorkingDirectory = Path.GetDirectoryName(#"C:\ProgramData\Anaconda3\envs\tensorflow1"),
UseShellExecute = false,
CreateNoWindow = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
try
{
x.Start();
var res = x.StandardOutput.ReadToEnd();
var err = x.StandardError.ReadToEnd();
Console.WriteLine(res);
Console.WriteLine();
Console.WriteLine(err);
Console.WriteLine();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadKey();
}
BUT the Process says I do not have some of the modules. Here they all are:
(notably, os, sys, numpy and cv2 are all understood. pandas and PIL, are not)
Python Code, (attempted Anaconda3, tensorflow1 envs)
import os
import cv2
import numpy as np
import sys
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import PIL
path = sys.argv[1]
I have untitled7.py in its own folder as a python file with its own solution, and they are both using the same Python environments, with the same module patches. Furthermore, untitled7.py has access to all the imports in the regular Python script, but not the .NET process.
It is clear that whatever I am doing, it's not calling the python file to run the same way. Is it because I am calling the Process incorrectly? Do I need to call a virtual environemt? Why can't I call the actual Python environment to run a Python script? Why does the exact same environment not provide the same set of modules for both solutions?
Thank you for your time.

Related

call python script in java application (Error in python import)

I call my python script with java and also pass variables. My The script alone in a PyCharm project works so far, only if I call it via Java (InteliJ) nothing happens.
The program should pass values ​​to my script via Java so that the script changes and saves values ​​in a Word Document (docx).
If I use the version of my script in the Intelij folder, it has problems importing docx.
I think the problem is that in the PyCharm project the docx data is directly in the project. This is not the case with Intelij and it should access the system data from docx.
I have already completely reinstalled lxml and docx but to no avail.
How do I have to change my program structure or my script so that it works.
Java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class runPython {
public static void main(String[] args) throws IOException {
try {
ProcessBuilder builder = new ProcessBuilder("python", "C://Users//Notebook//IdeaProjects//bhTool_bridge//scripts//main.py", "Annemarie Brekow");
Process process = builder.start();
}
catch (Exception e){
e.printStackTrace();
}
}
}
Python:
from docx import Document
import sys
import os
document = Document('C:/Users/Notebook/IdeaProjects/bhTool_bridge/template/Muster_Rechnung.docx')
workingdirectory=os.getcwd()
def find_replace(paragraph_keyword, draft_keyword, paragraph):
if paragraph_keyword in paragraph.text:
# print("found")
paragraph.text = paragraph.text.replace(paragraph_keyword, draft_keyword)
for paragraph in document.paragraphs:
find_replace("$Kunden-Name", "Annemarie Brekow", paragraph)
print(paragraph.text)
document.save("C:/Users/Notebook/IdeaProjects/bhTool_bridge/template/Muster_Rechnung.docx")
EDIT:
Errormsg
Traceback (most recent call last):
File "C:\Users\Notebook\IdeaProjects\bhTool_bridge\scripts\main.py", line 2, in
from docx import Document
ModuleNotFoundError: No module named 'docx'

How to bundle python code with vsix (vscode extension)

I am creating a vscode extension where I need machine learning tasks to be performed. I have python files that have code that is required in vscode extension. I don't want things to be done using request-response on any python server. What I want is to perform the ML tasks on device (integrated with the vsix).
We have child-process available in js to run basic python file using spawn. It is running fine on both, extension host window and external vscode editor after packaging, with the python code that has basic imports like import sys. But if I try to import some other libraries like numpy, pygments, it is working only on extension host environment, not on other vs window after packaging it. How can I run the typical python code with the vsix?
Below are both the codes that is working fine and not working at all-
TS file (MLOps.ts)-
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
import { join } from "path";
import * as vscode from 'vscode'
export async function pythonOps(): Promise<string> {
var result = "testt"
var promise = await new Promise((resolve, reject) => {
var p = __dirname.split('\\')
p.pop()
var path = p.join('\\')
var pyPath = join(path, 'src', 'py_operations.py')
var result = "blank result"
var arg1 = "arg one"
var arg2 = "arg two"
var py_process = spawn('python', [pyPath, arg1, arg2])
py_process.stdout.on('data', (data: any) => {
vscode.window.showInformationMessage(data.toString())
result = data.toString()
})
})
}
Working Python code (py_operations.py)- This code is working on both, on extension host window and after packaging the extension and installing the vsix on other system.
import sys
print("Some text with: ",sys.argv[0], sys.argv[1], sys.argv[2])
sys.stdout.flush()
Not working Python code- This code is working only on extension host window, and not working after packaging this and isntalling on other system.
import sys
from pygments.lexers.javascript import TypeScriptLexer
lexer = TypeScriptLexer()
src = "alert('text here')"
lexer_tokens = lexer.get_tokens(src)
l = []
for t in lexer_tokens:
l.append(t[1])
print("list: ",l)
sys.stdout.flush()
How can I run the second python code with packaged vsix?

Run python script with ML into C# console app error

I wanna run in C# console app python script with libraries like numpy, pandas, matplotlib.pyplot. If I run simple print('hello world') and save it as test.py it works.
I added to PATH : D:\ProgramData\Anaconda3\Library\bin but it didn't help me too. Thanks for try helping me!
Code:
// full path to .py file
string pyScriptPath = #"C:\Users\micha\Documents\ML\multipleLinearRegression\multiple_linear_regression.py";
string outputString = null;
// create new process start info
ProcessStartInfo prcStartInfo = new ProcessStartInfo
{
// full path of the Python interpreter 'python.exe'
FileName = #"D:\ProgramData\Anaconda3\python.exe", // string.Format(#"""{0}""", "python.exe"),
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
Arguments = #"C:\Users\micha\Documents\ML\multipleLinearRegression\multiple_linear_regression.py"
};
// start process
using (Process process = Process.Start(prcStartInfo))
{
// read standard output JSON string
using (StreamReader myStreamReader = process.StandardOutput)
{
outputString = myStreamReader.ReadLine();
process.WaitForExit();
}
}
Console.WriteLine(outputString);
console error
You said it works when you run the script with Python. Are you first creating an environment before running it? That may be the issue why it doesn't work when running it from C#.

Can't read a file in python using node

I'm running python in a node web app, and I'm trying to load and read a file in python, do something with it, then spit it out to node.js.
When I run the following python code, nothing happens.
Python
import json
import sys
with open('trainingData.json') as file:
data = json.load(file)
print(data)
print('hello from python')
sys.stdout.flush()
When I remove with open, then it works well. How can I read a file in python and call that file in node.js? Here's the node code
Node
app.get('/', (req, res) => {
const spawn = require('child_process').spawn;
const process = spawn('python', ['./python/script.py', 'Hello', 'World']);
process.stdout.on('data', data => console.log(data.toString()));
res.send('he');
});
(When I run the python file fro the terminal, it works correctly.)
You can use spawn's cwd (current working directory) option, to specify the directory. To set it to the "current" current working directory use __dirname.
const process = spawn('python', ['./python/script.py', 'Hello', 'World'], {cwd: __dirname});

Python py2exe window showing (tkinter)

I'm trying to make an exe by py2exe. The program is showing a popup-like window using Tkinter. The problem is, everything works fine when I run the setup like this:
setup(windows = [{'script': "msg.py"}], zipfile = None)
but it fails when I try to make an one-file exe:
setup(windows = [{'script': "msg.py"}], zipfile = None, options = {'py2exe': {'bundle_files': 1, 'compressed': True}})
Actually the final exe runs without problems, but it doesn't display any window. I've read there may be problems with bundle_files=1 at Windows 7, but I also tried bundle_files=2 with the same effect.
Here is my msg.py script:
from win32gui import FindWindow, SetForegroundWindow
from Image import open as iopen
from ImageTk import PhotoImage
from Tkinter import Tk, Label
from threading import Timer
from subprocess import Popen
import os
def Thread(t, fun, arg=None):
if arg<>None: x = Timer(t, fun, arg)
else: x = Timer(t, fun)
x.daemon = True
x.start()
def NewMessage():
global root
if not os.path.exists('dane/MSG'):
open('dane/MSG', 'w').write('')
root = Tk()
img = PhotoImage(iopen("incl/nowa.png"))
label = Label(root, image=img)
label.image = img
label.bind("<Button-1>", Click)
label.pack()
root.geometry('-0-40')
root.wm_attributes("-topmost", 1)
root.overrideredirect(1)
root.mainloop()
def Click(event):
global root, exit
root.destroy()
os.remove('dane/MSG')
OpenApp()
exit = True
def OpenApp():
hwnd = FindWindow(None, 'My program name')
if hwnd: SetForegroundWindow(hwnd)
else: Popen('app.exe')
root, exit = None, False
NewMessage()
Any ideas? I've read there are some problems with Tkinter, but there were about compilation. My script is compiled and it doesn't throw any exceptions, but doesn't show the window...
I ended up encountering this same issue, my solution involved doing the following:
Add
"dll_excludes": ["tcl85.dll", "tk85.dll"],
in your options = {...}
and then manually copy those two DLLs from
PYTHON_PATH\DLLs\ (in my case C:\Python27\DLLs)
to the location of your exe and try running it.
An alternative to dll_excludes and manual copying is to patch py2exe to know these files have to be placed directly in the dist directory.
Inside build_exe.py, there's a class called py2exe, which contains a list dlls_in_exedir for dll that have to go there. This list is set during a function named plat_prepare, and you can add the tclXX.dll and tkXX.dll files to it to make sure they are copied correctly.
Of course, unless you're the only one who will ever build this, you don't necessarily know which Tcl and Tk version you need to bundle - someone might have built their Python themselves, or are using an older Python with older DLLs. Therefore, you'll need to check which versions the system is actually using. py2exe actually already does this in a different place: by importing the internal _tkinter module (the actual Tk interface, usually a DLL) and accessing TK_VERSION and TCL_VERSION, which you can then use to generate and add the correct filenames.
If others are supposed to build your application, you probably don't want to make them modify their py2exe install, so here's how you can monkeypatch it from your setup.py:
import py2exe
py2exe.build_exe.py2exe.old_prepare = py2exe.build_exe.py2exe.plat_prepare
def new_prep(self):
self.old_prepare()
from _tkinter import TK_VERSION, TCL_VERSION
self.dlls_in_exedir.append('tcl{0}.dll'.format(TCL_VERSION.replace('.','')))
self.dlls_in_exedir.append('tk{0}.dll'.format(TK_VERSION.replace('.','')))
py2exe.build_exe.py2exe.plat_prepare = new_prep
This even works with bundle_files=1 on Windows 7.
If you have only one version the you can copy files with
via data_file. Below a full example:
WinXP
Python2.7.6
tk8.5
tcl8.5
tix8.4.3
py2exe 0.6.9
foo.py:
# -*- coding: iso-8859-1 -*-
import Tkinter
"""
sets TCL_LIBRARY, TIX_LIBRARY and TK_LIBRARY - see installation Lib\lib-tk\FixTk.py
"""
Tkinter._test()
Setup.py :
# -*- coding: iso-8859-1 -*-
from distutils.core import setup
import py2exe
import sys
import os
import os.path
sys.argv.append ('py2exe')
setup (
options =
{'py2exe':
{ "bundle_files" : 1 # 3 = don't bundle (default)
# 2 = bundle everything but the Python interpreter
# 1 = bundle everything, including the Python interpreter
, "compressed" : False # (boolean) create a compressed zipfile
, "unbuffered" : False # if true, use unbuffered binary stdout and stderr
, "includes" :
[ "Tkinter", "Tkconstants"
]
, "excludes" : ["tcl", ]
, "optimize" : 0 #-O
, "packages" :
[
]
, "dist_dir" : "foo"
, "dll_excludes": ["tcl85.dll", "tk85.dll"]
,
}
}
, windows =
["foo.py"
]
, zipfile = None
# the syntax for data files is a list of tuples with (dest_dir, [sourcefiles])
# if only [sourcefiles] then they are copied to dist_dir
, data_files = [ os.path.join (sys.prefix, "DLLs", f)
for f in os.listdir (os.path.join (sys.prefix, "DLLs"))
if ( f.lower ().startswith (("tcl", "tk"))
and f.lower ().endswith ((".dll", ))
)
]
,
)

Categories