Write print statements including errors/warnings to file in Spyder [duplicate] - python

This question already has answers here:
IPython: redirecting output of a Python script to a file (like bash >)
(8 answers)
Closed 4 years ago.
I have a big python script where multiple print statements and warnings (e.g. deprecation warnings) are being printed to the IPython console. For debugging purposes I want to write all print statements and warnings/error to an output file. Is there a way to do this in Spyder?
I have tried in the console (but it won't create an output file in any of my directories):
runfile('pyfile.py',wdir='wdir') > output.txt
I know how to do this in the prompt:
python "directory\pyfile.py"> output.txt 2>&1
But being able to do this in Spyder would be way more convenient. I have conducted an extensive search here on Stack Overflow but none of the answers to previous questions gave me the desired result.

(Spyder maintainer here) You need to be aware that Spyder uses IPython consoles to run your code, so it's always better to search for what things you can do in IPython instead of in Spyder.
In this case, searching for ipython save stdout gave me the right answer in the first result, and that's an StackOverflow one.
In essence, you need to use the %%capture magic or a context manager (as the one mentioned in the answer above) and put runfile inside one of them to capture all stdout output in your program.

Related

How to setup PyCharm to not erase variables after execute? [duplicate]

This question already has answers here:
PyCharm debug console not working
(10 answers)
Closed 1 year ago.
I'm starting on Python development using PyCharm and I want not erase the variables content after execute de file, like do Spyder to iteracte over data on Python console.
Like can see on the print the variable resultado is not available on window to set content.
resultado is a pandas data frame, there are some way yo see the CSV table on macOS instead see it on terminal?
PS.: I'm using Python 3.9 on venv.
The „Python Console“ here is basically just your typical Python REPL.
It doesn’t interact with the code you have written in the file; it’s basically an empty shell. That’s why it is unaware of your variabel.
You could run your file from the shell so it’s in context.
But most likely you want to run your file with the interpreter by using the little green arrow in the top right corner and the Python Shell tab on the bottom just for quick prototyping or to try stuff quickly as that’s more what it is intended to do.

Run Python-Script anywhere on Windows [duplicate]

This question already has answers here:
How to run a Python script portably without specifying its full path
(4 answers)
Closed 3 years ago.
I am looking for a way to run a python script from anywhere.
I have seen this solution but as it is based on Linux I am looking for a way on Windows (10).
Basically what I want to do is, execute a script with a given parameter. Because it could be disturbing for the user, I am using a .pyw-file extension to hide the console.
Things that came to my mind:
a PATH-Variable
PowerShell-Script
Batch-file
Sadly I am not familiar/experienced with neither of those, so I can't really tell if these ideas even provide a way to do that.
Any answer is appreciated.
Edit: I would like to make the command as short as possible for the client so it is not necessary to write a novel to exec one simple task.
Another Edit:
I want the user/client to open its cmd/ps and use a command which executes my python-script, which is not in this directory he is, when opening the cmd. So the script is somewhere on his computer, but shall be executable by command from anywhere.
try to create a "code.bat" file with this command "python script.py" inside your .bat file then add the "code.bat" to your path in your environments anytime you want to run the script just type code in your shell.

Creating a scikit-learn contribution package [duplicate]

This question already has answers here:
What does the $ mean when running commands?
(5 answers)
Closed 7 years ago.
As a beginner in Python, I'm reading a book written by Bill Lubanovic
I found something weird.
In that book, After saving simple code in test1.py, which is
print("This standalone program works!")
it says python can run it by typing in
$ python test1.py
However, whenever I try to use that, syntax error happens.
Although I know there are other methods like using exec() which I found in this website, I wanna know why book uses that method which doesn't work at least for me.
It means you need to type everything but the $ in the terminal.
python test1.py
It's just a convention though. Authors also use > python test1.py and other notations.
I don't know which version of his book you're reading, but he mentions it in this version.
In the example that follows, $ is a sample system prompt for you to type a command like python in the terminal window. We’ll use it for the code examples in this book, although your prompt might be different.
You are not supposed to enter the $.
The $ represents the shell/terminal prompt. This is the string of characters that appear in your terminal when it is waiting for input, although $ typically indicates some flavour of unix, e.g. linux.
Your terminal will probably use a different prompt, e.g.
[user#localhost ~]$
Or, if you are using a Windows terminal you might see :
C:\>
or
C:\WINDOWS>
Question was answered in following stackoverflow post:
What does the $ mean when running commands?
What does the $ mean when running commands?
As of now, Python does not implement $ in its syntax. So, it has nothing to do with Python.
Instead, what you are seeing is the terminal prompt of a Unix-based system (Mac, Linux, etc.)
So basically is terminal prompt and you should type in only: python test1.py without $ sign. another example is ~ when using oh-my-zsh.

What is '$' in python? [duplicate]

This question already has answers here:
What does the $ mean when running commands?
(5 answers)
Closed 7 years ago.
As a beginner in Python, I'm reading a book written by Bill Lubanovic
I found something weird.
In that book, After saving simple code in test1.py, which is
print("This standalone program works!")
it says python can run it by typing in
$ python test1.py
However, whenever I try to use that, syntax error happens.
Although I know there are other methods like using exec() which I found in this website, I wanna know why book uses that method which doesn't work at least for me.
It means you need to type everything but the $ in the terminal.
python test1.py
It's just a convention though. Authors also use > python test1.py and other notations.
I don't know which version of his book you're reading, but he mentions it in this version.
In the example that follows, $ is a sample system prompt for you to type a command like python in the terminal window. We’ll use it for the code examples in this book, although your prompt might be different.
You are not supposed to enter the $.
The $ represents the shell/terminal prompt. This is the string of characters that appear in your terminal when it is waiting for input, although $ typically indicates some flavour of unix, e.g. linux.
Your terminal will probably use a different prompt, e.g.
[user#localhost ~]$
Or, if you are using a Windows terminal you might see :
C:\>
or
C:\WINDOWS>
Question was answered in following stackoverflow post:
What does the $ mean when running commands?
What does the $ mean when running commands?
As of now, Python does not implement $ in its syntax. So, it has nothing to do with Python.
Instead, what you are seeing is the terminal prompt of a Unix-based system (Mac, Linux, etc.)
So basically is terminal prompt and you should type in only: python test1.py without $ sign. another example is ~ when using oh-my-zsh.

How to output in the log window in perforce client p4v using custom tools

We're developing perforce custom tools in python and we're outputting messages during the script execution. It shows up in p4win but we're mainly using p4v and the output doesn't show up in the log window.
Is there a way to output there or in any other pane without resorting to run the tool in a terminal window?
When Python is directing its output to a pipe rather than straight to a terminal, it buffers its output by default. I think you can work around this by either passing the "-u" parameter when invoking Python (e.g., python -u myscript.py arg1 arg2) to tell it not to buffer, or by calling sys.stdout.flush() throughout your script any time you want it to make sure that the output has made it to P4V.
See also:
http://kb.perforce.com/article/914/sending-script-output-to-p4vs-custom-tool-terminal
(It looks like that question was asked and answered after you asked here on Stack Overflow. Sorry if you're already well aware of it.)

Categories