PyCharm:如何正确连接到本地进程以进行调试

时间:2017-04-04 16:44:07

标签: python python-2.7 debugging process pycharm

您可以在此处关注PyCharm的文档:https://www.jetbrains.com/help/pycharm/2017.1/attaching-to-local-process.html我想附加到本地进程以进行调试。

所以我只是为了演示而在run.py文件中写了一些虚拟代码而没有任何实际用途:

package ui;

import java.util.List;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import model.DataModel;
import model.Person;

public class ConstrainedModelDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        DataModel model = new DataModel();

        List<String> guilds = IntStream.rangeClosed(1, 5).mapToObj(i -> "Guild "+i).collect(Collectors.toList());
        Random rng = new Random();
        List<Person> population = IntStream.rangeClosed(1,  100).mapToObj(i -> new Person("Person "+i, guilds.get(rng.nextInt(guilds.size())))).collect(Collectors.toList());

        model.getPopulation().setAll(population);

        TableView<Person> table = new TableView<>();
        table.setEditable(true);
        table.getItems().addAll(population);


        table.getColumns().add(column("Name", Person::nameProperty));
        TableColumn<Person, String> guildColumn = column("Guild", Person::guildProperty);
        guildColumn.setCellFactory(ComboBoxTableCell.forTableColumn(guilds.toArray(new String[guilds.size()])));
        table.getColumns().add(guildColumn);

        TableColumn<Person, Boolean> kingCol = new TableColumn<>("King");
        kingCol.setCellValueFactory(cellData -> {
            Person p = cellData.getValue();
            return Bindings.createBooleanBinding(() -> 
                p == model.getKings().get(p.getGuild()), p.guildProperty(), model.getKings());
        });

        kingCol.setCellFactory(tc -> new TableCell<Person, Boolean>() {
            private final CheckBox checkBox = new CheckBox();

            {
                checkBox.setOnAction(e -> {
                    if (checkBox.isSelected()) {
                        model.makeKing(table.getItems().get(getIndex()));
                    } else {
                        model.getKings().remove(table.getItems().get(getIndex()).getGuild());
                    }
                });
            }

            @Override
            protected void updateItem(Boolean item, boolean empty) {
                super.updateItem(item, empty);
                if (empty) {
                    setGraphic(null);
                } else {
                    checkBox.setSelected(item);
                    setGraphic(checkBox);
                }
            }
        });
        table.getColumns().add(kingCol);

        TableView<String> kingTable = new TableView<>();
        kingTable.getItems().addAll(guilds);
        kingTable.getColumns().add(column("Guild", guild -> new SimpleStringProperty(guild)));
        kingTable.getColumns().add(column("King", guild -> Bindings.createStringBinding(() -> {
            if (guild == null) return null ;
            Person king = model.getKings().get(guild);
            if (king == null) return null ;
            return king.getName() ;
        }, model.getKings())));

        BorderPane root = new BorderPane(table);
        root.setRight(kingTable);

        Scene scene = new Scene(root, 600, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> prop) {
        TableColumn<S,T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> prop.apply(cellData.getValue()));
        return col ;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我可以通过Pycharm附加到脚本中,我是从windows cmd开始的:python running.py

然而,PyCharm只显示它在控制台部分中实际打印的内容,而我无法在Pycharm中提供任何形式的源代码,任何关于为什么我不能步进或调试我的代码的想法解释器在IDE旁边的cmd中运行脚本时添加断点吗?

由于

[编辑] cmd中发生了什么:

import time

var_not_in_loop_below = 78
counter = 0
while True:
    counter += 1
    if counter <= 1:
        dummy_text = "Dummy"
    else:
        dummy_text = "Dummies"
    print(str(counter) + " " + dummy_text)
    time.sleep(2)
    loop_var = 42

在Pycharm中

$  C:\Python278\python.exe running.py  
1 Dummy                                
2 Dummies                              
3 Dummies                              
4 Dummies                              
5 Dummies                              
6 Dummies                              
7 Dummies                              
8 Dummies                              
9 Dummies                              
10 Dummies                             
11 Dummies                             
12 Dummies                             
13 Dummies                             
14 Dummies                             
15 Dummies                             
16 Dummies                             

想知道问题是否与以下这一行有关:

Attaching to a process with PID=8620
C:\Python278\python.exe C:\Users\eperret\AppData\Local\JetBrains\Toolbox\apps\PyCharm-C\ch-0\171.3780.115\helpers\pydev\pydevd_attach_to_process\attach_pydevd.py --port 62090 --pid 8620
Connecting to 32 bits target
Injecting dll
Dll injected
Allocating code in target process
Writing code in target process
Allocating return value memory in target process
Injecting code to target process
Waiting for code to complete
Connected to pydev debugger (build 171.3780.115)
Error when injecting code in target process. Error code: 7 (on windows)
10 Dummies
11 Dummies
12 Dummies
13 Dummies
14 Dummies
15 Dummies
16 Dummies

0 个答案:

没有答案