ipython runs older version of script first before running new version - python

I edited oldscript.py and then saved it in the same directory as newscript.py. After this, when I do %run newscript.py in ipython it seems to run oldscript.py before running newscript.py. I know this because it gives an output from oldscript.py before giving the outputs for newscript.py. It looks something like this:
%run newscript.py
output from oldscript.py
outputs from newscript.py
Why is it doing this? I've deleted the .pyc files, but that didn't help. I restarted ipython, my terminal, and my computer and nothing has changed. As far as I know, I don't have anything pointing to oldsript.py in newscript.py. I'm in the correct directory. I also tried running it in spyder and the terminal. Both give the same outputs. I feel like I've tried everything.
Also, I should mention that I'm new to python so there may be an obvious solution I haven't tried. Please advise :)

I'm pretty sure I just figured it out. I removed oldscript.py from the directory and it worked! Who knew Python could be so particular?! Okay, probably a lot of you, but give me a break I'm a noob ;)

Related

os.listdir() returns nothing, not even an empty list

During a presentation yesterday I had a colleague run one of my scripts on a fresh installation of Python 3.8.1. It was able to create and write to a csv file in his folder (proof that the csv library was working correctly), but everything else failed due to not being able to find the needed files. To try and isolate the problem and figure out why, we tried the below simple script, which also failed.
He had this test.py script in "D:/TEST", which also contained some folders and image files. Running this script printed nothing to the console. No empty list, no error message, no newline. Maybe the print() function was also not working, but I didn't get around to testing that.
import os
print(os.listdir())
This script works fine on my computer and my other colleagues computers (all Windows 10, similar hardware). I didn't have time to look into the issue more thoroughly and don't have access to his computer anymore. What could be the problem? What other things could I have him look into in order to fix this? In case this problem appears again during a future presentation, what steps could I take to figure out the cause of it?
My colleague uninstalled Python and reinstalled it. After doing this apparently the "python" command will no longer run his scripts, but using "py" instead will. Now that he is using "py" to run his scripts, it is working as expected.

Modifying information displayed in terminal (VSCode)

I have an aesthetic problem with my VSCode terminal. On this picture you can see, that whenever I try to run my code, terminal also displays information about the directory folder. It's annoying and completely useless to me... Is there a way to modify terminal settings, so that this shit is not displayed? After running the code I only want to see the result - in this case words "hellow world".
In some guides I've seen people having it that way, however I can't do it.
System: MacOs; Python 3.X
Thanks in advance!

Missing print() output to vs-code terminal

I'm relatively new to Python programming and have been using vs code. Up until very recently, while running a script, print() statement would appear in the vs code integrated terminal. But now they don't, and I can't figure out what happened. I've re-install vs code but no change, I've looked through the setting but can find anything.
If anyone can help, I would appreciate it.

Running Rscript via Python using os.system() or subprocess()

I am facing problems running a Rscript via Python using os.system() or subprocess().
Using os.system() to run commands via python works generally fine for me (e.g. with gdalwarp.exe) but not with Rscript.exe.
The only difference I can see are spaces in the path.
Avoiding problems with spaces in the path are easy overcome in the CMD-window by putting the paths in quotation marks.
Executing the following command is successfull.
"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"
But I am stuck with Python.
What I tried so far with python:
os.system("C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r'"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"')
subprocess.call([r'C:/Program Files/R/R-3.0.2/bin/Rscript.exe', r'D:/.../otsu_Script.R'])
Does anybody see what I am doing wrong?
Thanks in advance,
Eike
After getting mental on such a simple problem. I decided to reinstall RStatistics to a path with no spaces or points, like:
C:/R/bin/Rscript.exe.
Now
subprocess.call(["C:/R/bin/Rscript.exe", "D:/otsu_Script.R"] ) or
os.system("C:/R/bin/Rscript.exe D:/otsu_Script.R") are working just fine.
Should have tried it two days ago...
... but now I am a happy monkey anyway :-)
It probably is way too late now and I have seen you solved the issue, but I was having a similar issue (although in a Linux system) and it might help someone else now; this command was not working when called inside python although it worked directly on the terminal/command-line.
os.system("R CMD BATCH ./read_lengths_batch.R")
I tried many solutions, including subprocess and others but found it to be easier than that. In my case, and I understand it might be different in Windows, I just had to add a & at the end of the call for it to run in the background. Somehow it seemed R would shut down with the Python script instead of doing its work.
os.system("R CMD BATCH ./read_lengths_batch.R &")
Strangely, it was also working if in my folder I would have the same file copied with a .txt extension: read_lengths_batch.R and read_lengths_batch.txt.
Hope it helps someone!

Build command in sublime text has stopped functioning

I am a new user of Sublime text.
It has been working fine for a few days until it began to refuse to compile anything and I don't know where the problem is. I wrote python programs and pressed cmd+b and nothing happened. When I try to launch repl for this file - that also doesn't work. I haven't installed any plugins and before this issue all has been working well.
Any suggestions on how to identify/fix the problem are greatly appreciated
Yes, you might want to give more detail. Have you made sure you have saved the file as .py? Try something simple like Print "Hello" and then see if this works.

Categories

Resources