python - MacOS - Pty Host Disconnected VS Code - Stack Overflow

I am using VSCode to execute Python Code inline. The execution seems to work fine if I execute line by

I am using VSCode to execute Python Code inline. The execution seems to work fine if I execute line by line or couple of lines of code. Once I execute multiple code blocks the terminal gets unresponsive and then disconnects with error pty host disconnected

There is an existing question but the solution doesn't seem viable because even after I reconnect I have to start all over again as all the variable are gone. The same code works fine on a Windows machine.

What can be done to resolve this issue?

Other Details:

VS Code version: 1.98.0
MacOS: Sequoia 15.3
Apple Silicon M4

Adding sample code below for reproducibility. Disconnection always occur on if-else statement if I select the entire code below and run it inline.

import pandas as pd
import numpy as np
import scipy.stats as stats

# Set random seed for reproducibility
np.random.seed(42)

# Create a DataFrame with random data
data = {
    'Age': np.random.randint(20, 60, 100),
    'Height': np.random.normal(170, 10, 100),
    'Weight': np.random.normal(70, 15, 100),
    'Income': np.random.normal(50000, 15000, 100)
}
df = pd.DataFrame(data)

# Display basic statistics
print("Basic Statistics:")
print(df.describe())

# Hypothesis Testing: Is the mean height significantly different from 170?
height_sample = df['Height']
mean_height = 170

# Perform a one-sample t-test
t_stat, p_value = stats.ttest_1samp(height_sample, mean_height)
print("\nHypothesis Test: One-Sample t-test for Mean Height")
print(f"t-statistic: {t_stat:.4f}, p-value: {p_value:.4f}")

# Interpretation
alpha = 0.05
if p_value < alpha:
    print("Reject the null hypothesis: The mean height is significantly different from 170.")
else:
    print("Fail to reject the null hypothesis: No significant difference from 170.")

# Confidence Interval for Mean Weight
weight_sample = df['Weight']
mean_weight = np.mean(weight_sample)
std_error = stats.sem(weight_sample)
confidence_level = 0.95
conf_interval = stats.t.interval(confidence_level, len(weight_sample)-1, loc=mean_weight, scale=std_error)

print("\nConfidence Interval for Mean Weight:")
print(f"{confidence_level*100:.0f}% Confidence Interval: {conf_interval}")

# Correlation Matrix
print("\nCorrelation Matrix:")
print(df.corr())

I am using VSCode to execute Python Code inline. The execution seems to work fine if I execute line by line or couple of lines of code. Once I execute multiple code blocks the terminal gets unresponsive and then disconnects with error pty host disconnected

There is an existing question but the solution doesn't seem viable because even after I reconnect I have to start all over again as all the variable are gone. The same code works fine on a Windows machine.

What can be done to resolve this issue?

Other Details:

VS Code version: 1.98.0
MacOS: Sequoia 15.3
Apple Silicon M4

Adding sample code below for reproducibility. Disconnection always occur on if-else statement if I select the entire code below and run it inline.

import pandas as pd
import numpy as np
import scipy.stats as stats

# Set random seed for reproducibility
np.random.seed(42)

# Create a DataFrame with random data
data = {
    'Age': np.random.randint(20, 60, 100),
    'Height': np.random.normal(170, 10, 100),
    'Weight': np.random.normal(70, 15, 100),
    'Income': np.random.normal(50000, 15000, 100)
}
df = pd.DataFrame(data)

# Display basic statistics
print("Basic Statistics:")
print(df.describe())

# Hypothesis Testing: Is the mean height significantly different from 170?
height_sample = df['Height']
mean_height = 170

# Perform a one-sample t-test
t_stat, p_value = stats.ttest_1samp(height_sample, mean_height)
print("\nHypothesis Test: One-Sample t-test for Mean Height")
print(f"t-statistic: {t_stat:.4f}, p-value: {p_value:.4f}")

# Interpretation
alpha = 0.05
if p_value < alpha:
    print("Reject the null hypothesis: The mean height is significantly different from 170.")
else:
    print("Fail to reject the null hypothesis: No significant difference from 170.")

# Confidence Interval for Mean Weight
weight_sample = df['Weight']
mean_weight = np.mean(weight_sample)
std_error = stats.sem(weight_sample)
confidence_level = 0.95
conf_interval = stats.t.interval(confidence_level, len(weight_sample)-1, loc=mean_weight, scale=std_error)

print("\nConfidence Interval for Mean Weight:")
print(f"{confidence_level*100:.0f}% Confidence Interval: {conf_interval}")

# Correlation Matrix
print("\nCorrelation Matrix:")
print(df.corr())

Share Improve this question edited Mar 21 at 19:38 Lopez asked Mar 13 at 23:46 LopezLopez 4341 gold badge5 silver badges30 bronze badges 11
  • 1 Looks like the variables not reloaded is the an open issue but you can reload the variables manually using GetEnvironmentVariable command like says $env:<variable> = [System.Environment]::GetEnvironmentVariable("<variable>","User") – vht981230 Commented Mar 20 at 2:55
  • 1 The above command is a bit verbose but you can configure shortcut key with that command to run on the terminal – vht981230 Commented Mar 20 at 2:58
  • did but it didn't helped. – Lopez Commented Mar 21 at 18:24
  • Can you post a few lines of sample code you run to get that error ? – Philippe Commented Mar 21 at 18:52
  • I've added sample code with error screenshot. – Lopez Commented Mar 21 at 19:38
 |  Show 6 more comments

1 Answer 1

Reset to default 0

I've conducted some tests, and the issue is not with VSCode itself but with the Python interpreter. When running python3 in the macOS terminal, I encountered a similar problem—after a few lines (usually after an if-else statement), the text becomes corrupted, and the interpreter throws an Invalid Syntax error.

Here’s an example of the corruption:

weight_sample = df['Weight'weight_sample = df['Weigheight_sample)

The same issue occurs when running python3 in the VSCode terminal and pasting code.

Possible solutions:

  1. Use Python 3.13 – This version does not have the same bug, but lines are not executed automatically; you need to press Enter in the terminal. Maybe Python 3.12 also can work, I have no installed version.

  2. Report the bug to Microsoft and suggest running pasted code via the exec function, like this:

exec(r'''
...your pasted code...
''')

This method works without issues.

  1. Run the entire file instead of selected code – VSCode has an option to execute the entire file rather than just a selection.

  2. Use Jupyter in VSCode – This is a much better approach for running small experiments before writing actual code.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744679646a4587540.html

相关推荐

  • python - MacOS - Pty Host Disconnected VS Code - Stack Overflow

    I am using VSCode to execute Python Code inline. The execution seems to work fine if I execute line by

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信