bash VsCode and "autosave" - python

Morning guys,
I am a beginner programmer who was using PyCharm and recently moved to VsCode. Some days ago I started running scripts from Bash, but there is something that I do not understand why is not working properly.
Imagine I have a script called script.py and go to bash terminal and code:
$ python script.py
Ok, it runs correctly.
However, imagine that now I modify that script and i again I go to bash and code :
$ python script.py
And the result is that it runs the previous script and not the modified one!
I manually solve this problem by saving the script right clicking below OPEN EDITORS > script.py > Save
Thank you very much in advance.
Have a nice day!
I have tried to save it manually.

I do not understand why is not working properly.
It is working properly, just differently from what you're used to. You have to save the file before you can use it. I'm in the habit of pressing the key combination for Save All before I switch to the terminal window.
You can turn on auto-save by following the instructions here: https://code.visualstudio.com/docs/editor/codebasics#_save-auto-save

Related

How to not use the embedded console of pycharm

i would like to have a separate window open for the ouput of the console, like when i run my program without pycharm instead of the output going in the "run" tab of pycharm.
thanks in advance
First of all you didn't provide the information, I would have loved to see which OS you use or what are your preferences for visualizing your output which really needed to solve your problem so I'll answer your question generally.
If you are using windows you can use the command prompt also called the cmd to run your python file, using a simple WinKey + R and then writing cmd which will open a simple Command Prompt, then you will need to navigate to your python file path with the command cd which you can read about in the internet and then run python file_name or python3 file_name depending on what you have, this will give you your code output.
Using linux dist it will be very similar to the windows one pressing ALT+T will open the terminal for you which is like the twin brother for your cmd and then you'll need to follow the cd step and further in my first note.
Both ways will give you to run your code and will show you the output of your code without using pycharm.
Hope I helped :)

Beginner question about finding Python script in IDLE shell

I'm a beginner in Python with no prior programming experience, so bear with me here.
I installed Python, started playing with it (typing variables, playing with math operations) in the Shell window and everything is fine. I open a New Window and started writing a simple script. Something like this:
print (1+1)
I press Run Module, and I am asked to name and save the script first. I do so by calling it firstscript.py, and save it to my desktop.
Now I press Run Module, and in the shell window 2 is printed on the screen. Everything is fine. I close Python, and open it up again. Now in the shell window, I type firstscript.py and I get the red message NameError: name 'firstscript' is not defined.
I can run my program only if I open the script file on my desktop and press Run Module from there, but if I try to start it up directly in IDLE Shell by typing its name, I get the error message.
What did I do wrong? Thank you.
Good to see that you are starting with python.
Firstly, you can run the file directly using 'Run Module' only when you have the file open. Once you save the file and quit, you are out of the file editor and back to the terminal.
Simply typing in firstscripty.py will not work as it does not recognize the command.
To run the file from the terminal, use the below code:
python [locationOfFile\]firstscript.py
You can check out this detailed explanation: https://realpython.com/python-idle/#how-to-work-with-python-files
The problem here is the Shell doesnt know that your firstscript.py is sitting on the desktop
The simplest way i suggest using cmd with:
python C:\Users\{your user}\Desktop\firstscript.py

VS Code trying to run .../Activat.ps1 in cmd terminal

I'm using VS Code for a Python project using a virtualenv. I switched my deafult terminal from powershell to cmd as VS Code was not happy executing powershell scripts.
Now when I open a terminal in my project it opens cmd (as desired), but automatically tries tor run .../Scripts/Activate.ps1, which it doesn't like. I want it to run .../Scripts/Activate.bat as we are in cmd. Runnning it manually for now, but would be nice if I didn't have to.
No doubt there is a setting somewhere to change this, but I cannot find it. Any ideas?
This is a problem related to the Python extension, it should be fixed in the last update.
You can get some information from here.

How do force or change my terminal to run python executable file in python 3 just by opening the file?

My terminal is running python 2 so when I run my python file the program fails.
So basically I am new to programming and trying to create a small python script to help me auto create folders. I also want to give the script to colleges so that they can use it on their systems too.
I know that I can run my file in terminal by using "python3 myfile.py" and it will work, but that's too much off a mission to do for my colleges and as my colleges are not familiar with code or terminal for that matter, I wanted to create an executable file so that they just click to open type a few answers to the promoted question and boom folders created.
This is where I run into a problem, I have "#!/usr/bin/env python3" at the top of my file but when I run the script IDLE opens up and it just shows the code I have written but doesn't seem to run the actual script I wrote. Am I doing something wrong?
I also then though perhaps I could just use the terminal to run the file as it is now executable, so I go into terminal and enter "myfile.py" and the program runs but in python 2 so my script fails as it is in python3. So another question would be is there a way to code into my python file, when running this file make sure you use python3? as I would want this to work on all colleges system without them having to write out anything in terminal?
Sorry for the long explanation but any advice would be greatly appreciated.
Thank you in advance
When you are on windows you can just create a .bat file where you write: python3 myfile.py in it.
Both files have to be in the same directory.
If you want to have a .exe you can also use py2exe.
You could also try just #!/usr/bin/python3 without env.
More here.

Executing Python script without hanging terminal

Now this question might seem like an questions like this one and this one.
However unfortunately the above solutions don't work for me. I need a way to execute a Python 3.4.3 script, leave it running when the terminal closes, and have it not hang in terminal and executed directly.
pi#raspberrypi:/var/www/html/mysite/scripts $ nohup python3 my.script.py &
The above runs the script, but it hangs in terminal, I cannot enter any other commands until I stop it, by pressing CTRL + C.
Is there a way to achieve something like this:
pi#raspberrypi:/var/www/html/mysite/scripts $ nohup python3 my.script.py &
pi#raspberrypi:/var/www/html/mysite/scripts $
(Now I enter more commands, despite the script still running)
I hope I have provided enough information, hopefully you can help me, thanks!
Based on a quick google search, I found
start /b python3 my.script.py
for windows or
python3 my.script.py &
on lunix (bash).
If the ampersand doesn't work, you can use a terminal multiplexer like GNU screen or tmux for your purpose.

Categories

Resources