Linux how to write a python file without an IDE - linux

Make a copy of a computer python file you wrote for some other course without an IDE just a simple text editor
Modify this program slightly, without using an IDE.
Run the modified program, without using an IDE
How do I create a python file with linux?

To create a python file using Linux use command touch to create a file(will create a file in current directory, to know the current directory use cd command)
touch myfile.py
Open the file using one of the available text editors, for example vi:
vi myfile.py
Type your code and use command :wq to save and close the file.
a=2
b=3
print a+b
Run your code using python command:
python myfile.py
5 #output
To check or install Python on Linux, please refer to AWS detailed instructions: LINK
To get more instructions on using vi editor: LINK

It is as usual with writing c programs and others. No difference at all. If you are familiar with linux command line you can use command line text editors. Otherwise use gedit , sublime text,vs code,atom..etc (all are text editors with GUI) just as you use a notepad in windows.

Related

How to run a python file/script from sublime text 3 on cmd in windows 10?

I have been searching the internet up and down for a solution to this. I understand there is SublimeREPL which I can easily use to properly run python code in sublime text 3. However, it's not sufficient for me. I want to run my python file on the cmd from sublime text 3.
There are many reasons for me. The biggest reason is that i need to test my python files often and I don't want to cramp my sublime text workspace with thousands of tabs. There are several other reasons but at the moment I just want a solution. Almost everywhere on the internet it only talks about SublimeREPL, so I have been unable to find a solution.
I also understand I can use the cmd manually by going to the file directly and running it there, but its a pain in the back to keep switching to cmd and then to sublime text over and over again.
Therefore, I am looking for a neat solution where I can run my python file from sublime text 3 in cmd. Any help is deeply appreciated.
I am using python 3.7.3
The rule of thumb for build systems in Sublime Text is that if you can craft a command line that, when executed manually in the terminal/console, will give you the effect that you want (and that command doesn't require interactive input), then you can turn it into a build system.
In this case, what you want to do is spawn a new cmd window and do something inside of it; the fact that you're using Sublime is thus not interesting in the grand scheme of knowing how to do that, which might be why your search didn't turn up any results.
In Windows, you can use a terminal command like cmd /s /c something to tell the Windows cmd.exe that it should execute the command something. In your case you want to use Python to execute a program, so that might look something like the following to get Python to execute my_file.py.
cmd /s /c python my_file.py
However if you do that in an existing command prompt, the result is just to run the program in the current window; i.e. you're telling cmd to run a command, but it's still running in the current window, which is not what you want.
In order to run the program in a new window, you need to use the start command, which launches a new program. The general format of that would be something like this:
start "" cmd /s /c python my_file.py
That tells start to launch a new instance of cmd, which you're telling to run the program, so now the Python program is running in its own window.
There are some general issues with this; the biggest one is that as soon as cmd finishes executing the command you give it, it quits. Similarly, Python also quits when the program is finished. The result of that is that as soon as your program is finished, the window immediately vanishes before you can see what it did.
You could add -i to the python command to get it to go interactive, but then you'd have to interact with the internal python REPL in order to get it to quit and close the window, which it sounds like you don't want to have to take the step to do.
In that case you need to modify the command you give to cmd to get it to also wait until you press a key.
All told, an example of that might look something like the following as a complete sublime-build file:
{
"shell_cmd": "start \"\" cmd /s /c \"python -u \"$file\" & pause\"",
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python",
"env": {"PYTHONIOENCODING": "utf-8"},
}
This is a version of the internal Python.sublime-build modified to extend the command as outlined above. In order for this to work, you need to be able to enter python at a terminal and have it launch the Python interpreter. If that's not the case you need to adjust your PATH environment variable as appropriate.
If you're using Python 3, you may need to replace python with python3 or similar in the command so that cmd knows what to execute.

Run Perl script in Linux environment

I'm trying to run code on Linux environment
Here's the code (saved as hello.pl):
#!/usr/bin/perl
use strict;
use warnings;
print "Hello You\n";
Here's what I tried on my linux environment:
%perl hello.pl
I tried listing out the path starting from C:\Users\... and so on
I keep getting error that says:
Can't open perl script "hello.pl": No such file or directory
You have to be located in the same folder with the hello.pl in the "window" (aka terminal, or console) that you try to execute perl hello.pl.
On linux, you can determine the folder that you're in by issuing pwd.
If you're not in the same folder (the most probable cause of your error), you have 2 options:
Navigate to that folder with cd /path/to/your/script/location you have to replace the /path/to/your/script/location in the example, with your actual path
Execute the file with perl /path/to/hello.pl - of course, you have to replace the /path/to/ in the example, with your ac
Also, you can try and view the file from the console running a less hello.pl
In cygwin you might try: /cygdrive/c/Users/bonan/Desktop/perl/hello.pl.
Alternatively at your prompt try tying in just perl without hitting enter, and then drag the hello.pl file from its file explorer location into the terminal window. That should paste the full file path to the file as text into the command prompt. If you're using cygwin I forget it if properly pastes the path with forward-slashes, like /cygdrive/c/Users/bonan/Desktop/perl/hello.pl, or if it pastes what it would in cmd with backslashes as you've indicated you typed yourself.
The other thing to do that's relatively easy is right click the file and choose to open a terminal or shell here, which for cygwin you can get in your context menu by running chere -i once (it actually says "Bash prompt here" I think). And there's similar context menu options for cmd, powershell, an actual linux bash, or mac os x's terminal ... once you're in the same path as the file, you can just type perl heltab and autocomplete the filename assuming no other files in the same folder start with "hel".

Default file ending in Mac, Linux and Windows to run executable in the shell/terminal

I created executables of a python script (via pyinstaller) for Mac, Windows and Linux. For Linux and Mac, I am running them in the shell since it doesn't have an own interface: just open a shell and type the name of the program.
I am wondering if there is a way to use certain file ending so if the user clicks on the program, it will be automatically executed in the shell or terminal. Alternatively, I would appreciate any other ideas of how to do this.
The way to do this is not to append a certain file ending, but, as pointed out in the comment, make the file executable (chmod +x <file>) and add the magic bytes to the beginning of the file that tell the system how to execute it.
The magic bytes are #! and are followed by the path to executable. So for a python script you would put something like the following at the top of the file:
#!/usr/bin/env python
Okay, now I finally found out the solution to my question. All you have to do to execute the program upon clicking on it in the file browser is to add the ending .command and make it executable
E.g., exampleprogram.command. Clicking on it will execute the program in the shell

How do you launch matlab from the linux terminal while also opening a matlab file?

If I want to open file.m in the matlab editor, is there a way to do that directly from the linux terminal?
I can't seem to find the answer anywhere.
Start matlab with the following command to open a file:
matlab -r 'edit <filename>'

Edit a text file on the console using Powershell

I'm trying to figure out the easiest way to edit text files in the console (PowerShell in my case). I'm using Windows 7 64 bit. It galls me that I can't just type edit filename.txt to edit a file. That used to work, but that's all changed. What are my options to view and edit text files within the windows console, and if you tell me to install and learn VIM I'm going to punch you in the face. :-)
Why not use notepad?
notepad.exe filename.txt
The old edit.com works in PowerShell (at least on my box: Windows 7 Pro x86) but in x64 it doesn't work due to its 16bit architecture.
You can take a look at this easy editor.
Kinesics Text Editor.
It's super fast and handles large text files, though minimal in features. There's a GUI version and console version (k.exe) included. Should work the same on linux.
Example: In my test it took 7 seconds to open a 500mb disk image.
While risking you punching me, I guess you are stuck with the solution you mentioned. Have a look at this posting on SuperUser:
Which are the non-x text editors in Powershell?
Also, there is a nano version for windows:
Nano Editor
I'll duck and cover now, hopefully someone will have a more sufficient answer.
Bit of a resurrect but for anyone else coming to this question, take a look at the Micro editor. It's a small standalone EXE with no dependencies and with native Windows 32\64 versions. Works well in both PowerShell and CMD.EXE.
I agree with Sven Plath. Nano is a great alternative. If you have Chocolatey or scoop, you can install nano by typing the following in Powershell:
PS C:\dev\> choco install nano
# --OR--
PS C:\dev\> scoop install nano
Then, to edit somefile.txt enter:
PS C:\dev\> nano somefile.txt
It's pretty neat!
Edit:
Nano works well on my Windows 10 box but takes incredibly long to load the first time on my Windows 7 machine. That made me switch to vim (vi) on my Win 7 laptop
PS C:\dev\> choco install vim
PS C:\dev\> vim $profile
Add a line in the powershell profile to Set-Alias (sal)
sal vi vim
Esc - : - x - Enter :-)
If you use Windows container and you want change any file, you can get and use Vim in Powershell console easily.
To shelled to the Windows Docker container with PowerShell:
docker exec -it <name> powershell
First get Chocolatey package manager
Invoke-WebRequest https://chocolatey.org/install.ps1 -UseBasicParsing | Invoke-Expression;
Install Vim
choco install vim
Refresh ENVIRONMENTAL VARIABLE
You can just exit and shell back to the container
Go to file location and Vim it vim file.txt
You could install Far Manager (a great OFM, by the way) and call its editor like that:
Far /e filename.txt
You can install nano in powershell via choco - It's a low friction way to get text editing capabilities into powershell:
Install Choco
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Install Nano
choco install nano
Profit
nano myfile.txt
Best part is it becomes part of the path, and stays working across reboots etc :)
install vim from online, and then you can just do:
vim "filename" to edit that file
I'm thinking you could just use notepad, like this:
notepad myfile.extension
It should open in notepad.
I am a retired engineer who grew up with DOS, Fortran, IBM360, etc. in the 60's and like others on this blog I sorely miss the loss of a command line editor in 64-bit Windows. After spending a week browsing the internet and testing editors, I wanted to share my best solution: Notepad++. It's a far cry from DOS EDIT, but there are some side benefits. It is unfortunately a screen editor, requires a mouse, and is consequently slow. On the other hand it is a decent Fortran source editor and has row and column numbers displayed. It can keep multiple tabs for files being edited and even remembers where the cursor was last. I of course keep typing keyboard codes (50 years of habit) but surprisingly at least some of them work. Maybe not a documented feature. I renamed the editor to EDIT.EXE, set up a path to it, and invoke it from command line. It's not too bad. I'm living with it. BTW be careful not to use the tab key in Fortran source. Puts an ASCII 6 in the text. It's invisible and gFortran, at least, can't deal with it. Notepad++ probably has a lot of features that I don't have time to mess with.
Not sure if this will benefit anybody, but if you are using Azure CloudShell PowerShell you can just type:
code file.txt
And Visual Studio code will popup with the file to be edit, pretty great.
Well there are thousand ways to edit a Text file on windows 7.
Usually people Install Sublime , Atom and Notepad++ as an editor.
For command line , I think the Basic Edit command (by the way which does not work on 64 bit computers) is good;Alternatively I find type con > filename as a very Applaudable method.If windows is newly installed and One wants to avoid Notepad. This might be it!!
The perfect usage of Type as an editor :)
reference of the Image:- https://www.codeproject.com/Articles/34280/How-to-Write-Applet-Code
I had to do some debugging on a Windows Nano docker image and needed to edit the content of a file, who would have guessed it was so difficult.
I used a combination of Get-Content and Set-Content and base 64 encoding/decoding to update files. For instance
Editing foo.txt
PS C:\app> Set-Content foo.txt "Hello World"
PS C:\app> Get-Content foo.txt
Hello World
PS C:\app> [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("TXkgbmV3IG11bHRpDQpsaW5lIGRvY3VtZW50DQp3aXRoIGFsbCBraW5kcyBvZiBmdW4gc3R1ZmYNCiFAIyVeJSQmXiYoJiopIUAjIw0KLi4ud29ybGQ=")) | Set-Content foo.txt
PS C:\app> Get-Content foo.txt
My new multi
line document
with all kinds of fun stuff
!##%^%$&^&(&*)!###
...world
PS C:\app>
The trick is piping the base 64 decoded string to Set-Content
[System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String("...")) | Set-Content foo.txt
Its no vim but I can update files, for what its worth.
In linux i'm a fun of Nano or vim, i used to use nano and now vim, and they are really good choices. There is a version for windows. Here is the link https://nano-editor.org/dist/win32-support/
However more often we need to open the file in question, from the command line as quick as possible, to not loose time. We can use notepad.exe, we can use notepad++, and yea, we can use sublim text. I think there is no greater then a lightweight, Too powerful editor. Sublime text here. for the thing, we just don't want to get out of the command line, or we want to use the command line to be fast. and yea. We can use sublime text for that. it contain a command line that let you quickly open a file in sublime text. Also there is different options arguments you can make use of. Here how you do it.
First you need to know that there is subl.exe. a command line interface for sublim.
1-> first we create a batch file. the content is
#ECHO OFF
"C:\Program Files\Sublime Text 3\subl.exe" %*
We can save that wherever we want. I preferred to create a directory on sublime text installation directory. And saved there the batch file we come to write and create.
(Remark: change the path above fallowing your installation).
2-> we add that folder to the path system environment variable. and that's it.
or from system config (windows 7/8/10)
then:
then:
then we copy the path:
then we add that to the path variable:
too quick!
launch a new cmd and now you've got subl command working well!
to open a file you need just to use subl command as fellow:
subl myfileToOpen.txt
you can also use one of the options arguments (type --help to see them as in the image above).
Also note that you can apply the same method with mostly any editor of your choice.
You can do the following:
bash -c "nano index.html"
The command above opens the index.html file with the nano editor within Powershell.
Alternatively, you can use the vim editor with the following command
bash -c "vi index.html"
If you have windows subsystem for linux (wsl), you will find the following command very useful:
bash -c "vi filename.txt"
Sadly powershell doesn't come with a built in console-text editor.
You can redirect standard input for simple oneliners like so:
# write text and overwrite the file with that text
"my text that will appear in the file" > file.txt
For anything more complicated you will need a package manager Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
and install any of the following editors:
Nano
choco install nano
Issues
Very laggy on windows
Problems with arrow control
Vim
choco install vim
Issues
No major issues just remember esc + :!qa will exit vim

Resources