Executing python. I'm confused.
8 21 Jun 2015 22:07 by u/bohda
I learned the basics of C and could execute programs on the command line with no problem. When I try the most basic programs on the command line with Python, they are, er, not working right - I get wing-dings and shit. Obviously, I can compile them fine, but ./ -ing after compiling them gives me characters that I wouldn't expect with a C program. I'm not necessarily wanting to execute them on the command line, it's just the only way I know how to do it. What am I missing? Is there a some sort of framework or environment that I need? Again, I'm very basic with Python; syntax and functions. Simply, I just want to write a basic Python program and execute it. I use i3 on Linux but I also have gnome and when I go into gnome and use the interface to click on it, it says their isn't a program to execute Python bytecode. I'm just fucking lost here.
9 comments
1 u/Tommstein 21 Jun 2015 22:14
What are you doing, trying to run the .pyc files? Don't do that, run the .py files. The .pyc files are just a cache for the interpreter.
1 u/Vladar 21 Jun 2015 22:21
It'll be helpful if you could post some terminal output log of what is happening.
1 u/Master_Foo 22 Jun 2015 00:59
C and python are in no way similar. First of all, you don't need to compile Python. Just run the script. But first you need to do a few things.
step 1: Make sure your file has the "shebang" as it's first line. here is a sample "Hello World" program:
step 2: make sure your file is executable:
step 3: execute using
0 u/bohda [OP] 22 Jun 2015 06:49
You're a goddamn saint. It worked. Thanks a million.
1 u/bohda [OP] 22 Jun 2015 07:11
On a side note, and if you're willing to help a guy out, what's the reason for the difference? Why do I have to compile C but not have to compile Python? I guess I'm struggling with the 'why' and 'how' more than anything. From what I've heard, Python can be executed at run time. I don't exactly know what that means.
0 u/Master_Foo 22 Jun 2015 07:25
If you are asking these questions now, it probably means you don't have the background to really understand the significance of what I am about to tell you. The upside is, "Why" and "How" doesn't really matter. I don't reccommend worrying about the underlying mechanics just yet
But anyway, here are some pearls:
The difference is that C is a machine language and Python is an interpreted language.
The C compiler converts your C code into binary which your CPU can understand natively. It requires no interpreter. The benefit of this is the code is very efficient. But, you must compile a different binary for each architecture you want to support.
The Python interpreter, on the other hand, is compiled to "non-native byte code" (".pyc" files) which isn't architecture dependent. That code is executed inside of an interpreter which is similar to a virtual machine. Since it is the virtual machine which runs the instructions you don't need to compile for each architecture. However, the cost that there is a lot of overhead so python programs are much more CPU and memory intensive.