No Output Displays When Execute Python File

No output when executing python file from c#

I can't directly answer to your question because I have no python installed and can't reproduce the problem. But I'll show how to do the same thing completely with C#.

I assume that the C# project is WinForms.

Install 2 NuGet packages (in Visual Studio menu Project -> Manage Nuget Packages)

  • HtmlAgilityPack
  • Fizzler.Systems.HtmlAgilityPack

First one provides HTML parser, 2nd provides an extension HtmlNode.QuerySelector. Query syntax for QuerySelector is almost the same as in JavaScript.

After installation add namespaces to the code.

using HtmlAgilityPack;
using HtmlDocument = HtmlAgilityPack.HtmlDocument; // overrides class name conflict with System.Windows.Forms.HtmlDocument
using Fizzler.Systems.HtmlAgilityPack;

And whole code of the project

public partial class Form1 : Form
{
private static readonly HttpClient client = new HttpClient();

public Form1()
{
InitializeComponent();
}

private async void button1_Click(object sender, EventArgs e)
{
try
{
label1.Text = "Connecting";
using (HttpResponseMessage response = await client.GetAsync("https://fred.stlouisfed.org/series/AAA", HttpCompletionOption.ResponseHeadersRead))
{
response.EnsureSuccessStatusCode();
label1.Text = "Receiving data";
string text = await response.Content.ReadAsStringAsync();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(text);
label1.Text = doc.DocumentNode.QuerySelector("div.pull-left.meta-col span.series-meta-observation-value").InnerText;
}
}
catch (Exception ex)
{
label1.Text = "Error";
MessageBox.Show(ex.Message + "\r\n\r\n" + ex.StackTrace);
}
}
}

enter image description here

Output is not showing in python

>>> import test
>>> print(test)
<module 'test' from '/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/test/__init__.py'>

There is a test module in Python itself. Be sure you are running your module. You must be in the same directory as your file test.py to import your file instead of the Python one.

Update: Python uses a search path list mechanism for finding modules when importing. The "current working directory" is usually at the first place in this list (the empty string). See PYTHONPATH and sys.path.

You can add your own path(s) into PYTHONPATH (in shell/command line) or sys.path (in Python).

How to keep a Python script output window open?

You have a few options:

  1. Run the program from an already-open terminal. Open a command prompt and type:

    python myscript.py

    For that to work you need the python executable in your path. Just check on how to edit environment variables on Windows, and add C:\PYTHON26 (or whatever directory you installed python to).

    When the program ends, it'll drop you back to the cmd prompt instead of closing the window.

  2. Add code to wait at the end of your script. For Python2, adding ...

    raw_input()

    ... at the end of the script makes it wait for the Enter key. That method is annoying because you have to modify the script, and have to remember removing it when you're done. Specially annoying when testing other people's scripts. For Python3, use input().

  3. Use an editor that pauses for you. Some editors prepared for python will automatically pause for you after execution. Other editors allow you to configure the command line it uses to run your program. I find it particularly useful to configure it as "python -i myscript.py" when running. That drops you to a python shell after the end of the program, with the program environment loaded, so you may further play with the variables and call functions and methods.

No output in Output Panel when running code - Python on VSCode

If you want to display the results in the debug console, you could open file launch.json in folder .vscode and add this setting: "console": "internalConsole",. (or open the setting pattern next to the debug button to open launch.json file. )

enter image description here

This is the complete content of my launch.json file:

{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole",
}
]
}

Then, through the test, the result is displayed in the debug console. like this:

enter image description here

Reference: Python debug configurations in VSCode.

Python script won't run from the command line. It shows no error

python isn't in your path. Checkout Adding directory to PATH Environment Variable in Windows, which is a good reference to the question of adding a variable to the path in CMD.



Related Topics



Leave a reply



Submit