Same answer as @jil however, that answer is specific to some historical version of Python.
In modern Python (3.x):
exec(open('filename').read())
replaces execfile('filename') from 2.x
It can be used with a context manager, as mentioned by MartinUeding, to ensure the file closes:
with open('filename') as f:
exec(f.read())
Answer from Hack-R on Stack OverflowSame answer as @jil however, that answer is specific to some historical version of Python.
In modern Python (3.x):
exec(open('filename').read())
replaces execfile('filename') from 2.x
It can be used with a context manager, as mentioned by MartinUeding, to ensure the file closes:
with open('filename') as f:
exec(f.read())
You could use execfile:
execfile("/etc/default/foo")
But please be aware that this will evaluate the contents of the file as is into your program source. It is potential security hazard unless you can fully trust the source.
It also means that the file needs to be valid python syntax (your given example file is).
Sourcepy: source python files straight from the shell
python - Get location of the .py source file - Stack Overflow
can someone explain what is the python file like Python shell and window … ect , im confused
Is there a way to get the source code of a module?
Hey folks, sharing a project I built just for fun, Sourcepy: https://github.com/dchevell/sourcepy
I was writing some increasingly messy shell functions and thought it would be nice if I could just write Python functions/variables/etc. instead and source them from the shell. So … that's what this is. It uses a mix of importlib and inspect machinery to "source" files (in reality, generating shell stubs that hook back into a bridging interface, and aliasing `source` so it works just like you’d expect).
Along the way I ended up deep diving into type annotations and turning plain python functions into dynamic command line apps that can coerce to native values, handle stdin, work with a large variety of types & objects and do a number of other interesting things - all without your code having to know a thing about Sourcepy. You write plain old python functions, add typehints to the parameters for the best results, and in most cases it should "just work".*
This is still a work in progress - its use case is largely for folks like me who like excuses to mess around in Python and customise their environment and toolset to an unnecessary degree.
Try it out, share any suggestions (or bugs, or criticism). I've poked around looking for similar projects and the only similar thing I could find required running a daemon which wasn't an approach I liked, so hopefully you'll find some novelty here.
*not a guarantee
I know I can use the help method or the inspect library to inspect other modules/libraries, the problems with those methods is that they print the code into my terminal, I want a method that lets me change the code and manipulate in any way I see fit, so I basically want the file of the code.
I downloaded a file, only to discover that it lacked an executable (exe). It turns out it was the source code. Could you kindly guide me on the process of compiling it?
If you enable the
source coderepositories in "Software sources" application (software-properties-gtk), you can download the latest python source that is related to the package:apt-get source pythonYou can do that for any ubuntu/debian package.
You can always google your way to the source code: http://www.python.org/download/releases/
Try from command line:
dpkg -L python2.7It shows you where the files are installed in your system.
Try to install the package python2.7-examples. It contains many useful examples for learning purposes.