Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
It will great help if you can explain with code example or any useful resource.
you can use the in-built pdb library.
import pdb
a = 10
pdb.set_trace()
print(a)
So what it would basically do is, stop the program after line 2 is executed. and from there you would have command line access to the code with is execute above the set_trace() is added.
You can read more about it here.
The breakpoint() method is part of the python language now. See: https://www.python.org/dev/peps/pep-0553/
If you are using an earlier version of python then you can have the same functionality by importing pdb by import pdb at the top of you file and pdb.set_trace() where you want to be able to inspect variables.
When you are dropped into the pdb shell you type h to see what commands are available.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I am trying to save everything my python app printed in the cmd, every solution I found was to get the response of a command line using Popen from Subprocess; what I want is to get everything. Is there a built-in function for this purpose? or should I do it manually, which I don't prefer?
What you want to do, is log the output of stdout, see this answer:
Making Python loggers output all messages to stdout in addition to log file
Why don't you use this from terminal
python main.py > output.log
you can try the script command in your shell. It saves everything printed out to your shell from the moment you call it. End the command with exit. you can call it from your python script, hope it helps.
script manual page
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I would like to ask you for a starting point or an idea to search for information on the subject. I have found many post about "how to run R script in Python", but I just need to use a function inside a specific package. So, I would like to know if it is possible to invoke a R function, using my data in Python and get the output. Thanks in advance!
I would look into rpy2. It takes a minute to set up but you can call R packages and use their functions fairly easily. All you have to do is use importr followed by the package you want to use from there. A few places to get started are:
https://rpy2.github.io/doc/v3.0.x/html/robjects_functions.html
https://medium.com/analytics-vidhya/calling-r-from-python-magic-of-rpy2-d8cbbf991571
Best of luck.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am working on a Python Project and want to call multiple functions from other python files, but I'm having trouble. Can someone please help me?
Each of your Python file can act as a module, that you can import.
for example for numpy you just type 'import numpy' and then you'll be able to use numpy.sin() function.
by default you can only append modules located in sys.path. So What you can do is:
import sys
if not (<folder_with_your_function> in sys.path):
sys.path.append( <folder_with_your_function> )
import <your_function>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a set of scripts that each have their own specialized termination functions. Many of these scripts call a module of general functions. Some of these module functions should direct the script to terminate.
I am aware that the standard approach would be to have the module functions return values that are interpreted by the scripts (in this case, in a way that causes the scripts to terminate), but I would like to know how to call the terminate function of a script from the module.
Your scripts should register their special termination functions using atexit.
(See https://docs.python.org/3.5/library/atexit.html)
Then they will be called no matter why it terminates.
Your general module can then just use sys.exit().
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is it generally a good idea to put a console client into module's if __name__ == "__main__": section? E.g. code to setup argparser and preprocess user input.
With this, a program will effectively double as a module and a script. There's nothing wrong with this per se. The pdb standard module is an example and e.g. programs that run as Windows services using win32serviceutil.ServiceFramework are usually done this way.
Your only concern here is if you have several scripts and/or modules, you can still tell which is which. See Is a Scripts directory an anti-pattern in Python? If so, what's the right way to import? for a related discussion.