Pycharm interactive console not working

I am new to both python and Pycharm. Thus, please feel free to indicate where I made a mistake and how I can solve the problem.

The problem is that IPython cannot import the functions that I want to execute as usual. Even after running the python file, I cannot import the functions from this file into the IPython console. In addition, the IPython console did not complete code execution.

For example, I write a python file called student.py in which I define a class called student . Then I ran this file. But the IPython console says that the class 'student' is not defined when I type student('Jack', 28) in the console.

 class student(object): def _init_(self, name, age): self.name=name self.age=age 

What scares me is that I can run the file. But when I type student('Jack', 28) in the console, the IPython console reports

 Traceback (most recent call last): File "/Library/Python/2.7/site-packages/IPython/core/interactiveshell.py", line 3032, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-483e7a488507>", line 1, in <module> student('Jack',28) NameError: name 'student' is not defined 

What's more, IPython Magic Function also does not work in the IPython console.

 In[3]: %run student.py ERROR: File `u'student.py'` not found. 

Sorry for the lack of pictures to make situations clearer due to lack of reputation.

+5
source share
2 answers

It depends on how you run the python file. There are many ways to do this from within pycharm (perhaps too many).

I assume that you are doing this pushing the green triangle. This does not execute the file in the same shell that exists on the Python Console tab at the bottom. Instead, it launches a new shell, executes the file, and by default closes the shell when the file is executed. You can see how this shell does this on the Run tab below. As seen from Schivendra’s answer, there may be a way to avoid killing the shell in the script output. If this is true, then you are using a terminal / shell that remains on the Run tab and not on the Python Console tab.

This is very similar to what happens if you use Debug instead of Run. It starts the interpreter, attaches the debugger, runs the script, kills everything when it is done. It is located on the Debug tab.

The easiest way to achieve what you mean is to run the file in an existing Python Console as follows. In this case, the script runs as if it were __main__ , so if you have if __name__ == "__main__": it will evaluate to True, and any code in the if block will also be executed. (More on this later if you do not know what it is now)

 In[2]: dir() Out[2]: ['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__name__', '__package__', '_dh', '_i', '_i1', '_i2', '_ih', '_ii', '_iii', '_oh', '_sh', 'exit', 'get_ipython', 'quit', 'sys'] In[3]: run -m conventions.iec60063 In[4]: dir() Out[4]: ['Decimal', 'E12', 'E24', 'E3', 'E6', 'In', 'Out', '_', '_3', '__', '___', '__builtin__', '__builtins__', '__doc__', '__loader__', '__name__', '__nonzero__', '__package__', '_dh', '_i', '_i1', '_i2', '_i3', '_i4', '_ih', '_ii', '_iii', '_oh', '_sh', 'cap_ostrs', 'elem', 'exit', 'gen_vals', 'get_ipython', 'get_series', 'idx', 'ind_ostrs', 'quit', 'res_ostrs', 'sys', 'zen_ostrs'] 

My preferred way to “run” the file is to simply import it from the “Python shell” (IPython or otherwise) and manually run any initialization code that needs to be run. The advantage of this is not to overly pollute the namespace and present an environment that is much closer to what you would expect if you would use the script as a module (where pycharm and IPython and autocomplete really start to pay off). In both methods, You have the option of connecting a debugger to the Python Console interpreter using the Attach Debugger icon next to the shell (green error).

A brief example is as follows (on the Python Console tab):

 In[2]: dir() Out[2]: ['In', 'Out', '_', '__', '___', '__builtin__', '__builtins__', '__doc__', '__name__', '__package__', '_dh', '_i', '_i1', '_i2', '_ih', '_ii', '_iii', '_oh', '_sh', 'exit', 'get_ipython', 'quit', 'sys'] In[3]: import conventions.iec60063 In[4]: dir() Out[4]: ['In', 'Out', ... (all the same ones as before) 'conventions', ... (all the same ones as before) ] 

In fact, you will not run the file as such, so you should know what you are doing.

When you execute the python file, the module considers that __name__ is equal to "__main__" , and this is the beginning of the trick if __name__ == "__main__": (which you must find at an early stage in the process of learning python) if you then try to execute code execution, the interpreter will execute everything in a module that is not a class or function. This includes the if __name__ == "__main__": condition, which you'll see in many python scripts. The contents of the if block are executed only if the script is executed by itself (or using the run -m module in IPython)

On the other hand, when importing a module, the same sequence of execution occurs, except for the fact that if __name__ == "__main__": evaluates to False, and any code in this block will not be called. Therefore, you need to manually execute everything that is in the if __name__ == "__main__": block if __name__ == "__main__": you need it. One common template to simplify this task is to simply have a minimal function call in the if block (or, if you care about command line arguments, just process them there), which then gives up the bulk of the function execution to an easily called function:

 def main(): pass if __name__ == "__main__": main() 

If you end up importing a module, but you still need to execute this code, all you need to do is call the main () function. In the above example, what I would do (if I had to execute some code when the module “starts”) would look something like this:

 In[2]: import conventions.iec60063 In[3]: conventions.iec60063.main() 

Pycharm has several more exotic ways to execute code (to execute only a small fragment of a file, etc.), which I don’t really need, so I don’t quite understand how they will work.

+1
source

To save objects / classes / functions / variables even after execution, you will have to change the interpreter option in Run> Edit Configuration> Interpreter Option , you need to add -i . I still don't know about code completion in the console.

+2
source

Source: https://habr.com/ru/post/1216411/


All Articles