i want to code in python and i know the syntax well.. but i have got no idea how to compile and run it ..!! i mean i am from ruby , java , c , c++ background and there after saving it in a file we go to command prompt and type the command and the file name to compile and run it .
then what about python ?
why does python filename.py doesnt work ?
and how to make a file run then ?
and which python books to follow for better understanding
i am using windows os .! and i dont want to run it line by line on the idle .. i want to write the whole code and then run it from windows command prompt
If you're using Windows, you'll need to add the path to your Python executable to the Path environment variable; on Linux, and I presume Mac, this should already be done.
Oh, and you don't compile python programs, they are interpreted at run time.
If you are from Ruby background, you should be able to handle another interpreted language, which is what python is too.
Good starter resource:
Dive into Python
what operating system are you using?... you dont need to compile python code its interprated. just invoke the command line interpreter followed by the name of your .py file
Related
Excuse the awkward question wording.
I've made a script. I would like for others to download it from github, and run it by typing programName argument1 argument2, similar to any other popular app used through the terminal such as Jupyter or even opening Atom/Sublime/etc. (ex:jupyter notebook, atom .). However, unlike Jupyter or sublime, my script isn't launching another app, it's a small app meant to be used in the shell.
Currently, to use my script, one must type into the command line python programName.py arg1 etc from within the file's directory.
How do I allow others to dl it and use it from anywhere (not having to be within the directory), without having to type out the whole python programName.py part, and only having to type programName arg1?
This blog post explains step by step how to create a distribution that you can install and it would turn into an executable.
You can refer to this github repo for a sample application.
The full documentation of setuptools is available here.
In general, you should configure your setup.py in order to use the command in the entry-point option:
setup(
name = "your_app_name",
packages = ["package_name"],
entry_points = {
"console_scripts": ['cmd_name = package_name.package_name:main']
},
....
)
This solution would work on every OS where you have installed python.
Your script may need to have an interpreter, "shebang", besides being "reachable" by the $PATH
#!interpreter [optional-arg]
For example, you could have something like
#!/usr/bin/env python
or to force a specific version
#!/usr/local/bin/python2.7
Next, your script needs to be available within the $PATH, check this answer that covers that part: https://unix.stackexchange.com/q/29608/53084
You can simply add your script to PATH variable in order to launch it from anywhere.
In Linux distros, you can simply do it by using a bash command PATH=$PATH:/path/to/your/script.
Make sure you don't have the space around the "=" operator.
Now, the second thing is you don't want your script to be named as pythonProgram.py.You can simply remove the extension .py from PythonProgram.py by adding a single line to the starting of your script.
Open up your script and at the very begining type #!/usr/bin/python.This should be the first line of your code.This line is called shebang and is used to tell the bash which interpreter to be used for compiling the script.
If everything went right, you will be able to run your script as pythonProgram arg1.
In addition to mabe02: Python is a scripting language and usually not compiled, which means you will need an interpreter to run your program.
Programms made in C f.e. can be run on its own because they were compiled into machine code by a compiler.
An interpreter is similar to a compiler as it reads your script and interprets it at runntime. That is why you need to write python before your programm, to tell your computer to interpret your script on runntime, using python. This doesn't mean that there are no possibilities to compile python as can be seen in the other answer and in this link Can a python program be run on a computer without Python? What about C/C++? (py2exe and py2app).
I was going through Google's Python instructions and it quotes:
The commands above are the simplest way to run python programs. If the
"execute bit" is set on a .py file, it can be run by name without
having to type "python" first. Set the execute bit with the "chmod"
command like this:
Where it proceeds to give me this code to run on my terminal:
~/google-python-exercises$ chmod +x hello.py
~/google-python-exercises$ ./hello.py ## now can run it as ./hello.py
Hello World
However when I type in ./hello.py, I get:
So is this something to do with me using Python 3 when this could be a command for Python 2?
EDIT
I just realised that this can only be run on a Linux/Mac. How does one go about doing this on a Windows?
You need to associate the file extension with Python properly. This should happen by default if you install system wide (which requires administrator privileges), but if you're using a third party distribution, it's not guaranteed, and it may require a reboot even so.
For more details running Python on Windows, see the Python on Windows docs. It looks like if you omitted the launcher from your Python install, it won't have registered the file extensions; it may be worth reinstalling properly to fix that.
Otherwise, if you can't get file extension associations working, just run:
python hello.py
or starting with 3.3 and later, you can use:
py hello.py
which lets you provide an argument to py.exe to change which installed version of Python to use (if desired).
You wouldn't use ./hello.py on Windows at all, because that's the wrong directory separator (Windows sometimes allows forward slashes, but in DOS prompts, that looks like trying to launch a program named . with the switch /hello.py). You'd use plain hello.py (Windows always searches the current working directory) or .\hello.py if you want to be explicit.
Do you have a shebang as the first line in hello.py?
something like:
#!/usr/bin/env python3
I would like to transform blender meshes into ogre meshes. I downloaded this add-on which is a python script that allows me to do it.
I already installed this extension in blender (user preferences - addons - Install) and works perfectly.
Now I would like to execute the same script in the command line so I can transform any mesh without opening the blender user interface.
Using cmd (I'm a windows user) I go to the folder where blender is installed and run the following command:
blender.exe --background --python ./2.76/scripts/addons/io_export_ogreDotScene.py box.blend
but nothing happens. I think it's because the script was written only to be used inside blender, not through the command line.
Is there a workaround to execute this script ?
I'm not really good in python, so reading the script is not helping me at all.
I'll appreciate any help I can get form you guys !!
I was able to decode the script and make it run from the command line. It converts a blender file sent as a parameter and converts it in a ogre mesh file.
If you need it, let me know.
I'm trying to learn python but have some problem running source files from power shell. When I type 'python' it opens up and I can type python commands directly in the shell. I think this is called interactive mode. But when I try to run/execute a source file I get an error message: It sayys: Syntax error: invalid syntax.
I use 'python myfile.py' when I try to execute the script.
If I run the same file from IDLE it works just fine. Ana idea what I'm doing wrong?
Here is myfile.py. I'm running python 2.7
# filename: myfile.py
while True:
s = raw_input('Enter something: ')
if s == 'Quit':
break
print 'Lenght of the string is', len(s)
print 'Done'
You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type >python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g >python C:\myfile.py.
If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g. >myfile.py
I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just >myfile should work.
Edit after Update:
Typing just >python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.
You will need to put the full path of the Python executable within the command line in order for it to work. You could check and ensure that your python exe is included in your Path among your system variables.
Disclaimer: I don't know PowerShell, but I do know cmd.exe.
I don't know why python myfile.py doesn't work, but assuming that PowerShell bears at least some similarity to cmd.exe, the following should probably work: myfile.py. That's right, just enter the name of the Python script and hit enter.
If you started by typing "python" in powershell you will need to get out of that script.
If you are in python type:
quit()
then type
python myfile.py
This should work if your python is installed correctly.
Try to type this in Powershell:
$env:path="$env:Path;C:\Python33
After this, command
python yourfile.py
should work.
This my sound silly, especially coming from a beginner.
Just save the file on your desktop. Open up powershell and drag the file directly into powershell and it opens. kind of tedious but it works
How do I run a Python file from the Windows Command Line (cmd.exe) so that I won't have to re-enter the code each time?
Wouldn't you simply save your Python code into a file, and then execute that file using Python?
Save your code into a file called Test.py.
And then run it?
$ C:\Python24\Python.exe C:\Temp\Test.py
If you don't want to install an IDE, you can also use IDLE which includes a Python editor and a console to test things out, this is part of the standard installation.
If you installed the python.org version, you will see an IDLE (Python GUI) in your start menu. I would recommend adding it to your Quick Launch or your desktop - whatever you are most familiar with. Then right-click on the shortcut you have created and change the "Start in" directory to your project directory or a place you can mess with, not the installation directory which is the default place and probably a bad idea.
When you double-click the shortcut it will launch IDLE, a console in which you can type in Python command and have history, completion, colours and so on. You can also start an editor to create a program file (like mentioned in the other posts). There is even a debugger.
If you saved your application in "test.py", you can start it from the editor itself. Or from the console with execfile("test.py"), import test (if that is a module), or finally from the debugger.
If you put the Python executable (python.exe) on your path, you can invoke your script using python script.py where script.py is the Python file that you want to execute.
Open a command prompt, by pressing Win+R and writing cmd in that , navigate to the script directory , and write : python script.py
A good tool to have is the IPython shell. Not only can it run your program (%run command), but it offers also many tools for using Python interactively in an efficient manner (automatic completion, syntax coloring, quick access to the documentation, good interaction with Matplotlib,…). After you install it, you'll have access to its shell in the Start menu.
You need to create environment variables. Follow the instructions here: http://www.voidspace.org.uk/python/articles/command_line.shtml#environment-variables
In DOS you can use edit to create/modify text files, then execute them by typing python [yourfile]