Running a python program in Windows? - python

I have Python 3.6 and Windows 7.
I am able to successfully start the python interpreter in interactive mode, which I have confirmed by going to cmd, and typing in python, so my computer knows how to find the interpreter.
I am confused however, as to how to access files from the interpreter. For example, I have a file called test.py (yes, I made sure the correct file extension was used).
However, I do not know how to access test.py from the interpreter. Let us say for the sake of argument that the test.py file has been stored in C:\ How then would I access test.py from the interpreter?

The simplest way would be to just do the following in cmd:
C:\path\to\file\test.py
Windows recognizes the file extension and runs it with Python.
Or you can change the directory to where the Python program/script is by using the cd command in the command prompt:
cd C:\path\to\file
Start Python in the terminal and import the script using the import function:
import test
You do not have to specify the .py file extension. The script will only run once per process so you'll need to use the reload function to run it after it's first import.
You can make python run the script from a specific directory:
python C:\path\to\file\test.py

In command prompt you need to navigate to the file location. In your case it is in C:\ drive, so type:
cd C:\
and then proceed to run your program:
python test.py
or you could do it in one line:
python C:\test.py

I may be misunderstanding what you are seeking, but I think what you are asking for is a IDE-like environment where you can load Python scripts, edit, and debug.
You already have IDLE, https://docs.python.org/3.6/library/idle.html, which came with the Python installation.
There are many IDEs available for Python. https://wiki.python.org/moin/IntegratedDevelopmentEnvironments Personally, I like using PyDev on Eclipse.

Related

How to run the python shell inside a terminal in Windows?

I've scrolled the internet all over and theres no simple, clean explanation for this. How can I open the Python shell, but inside a terminal window such as CMD? I have an application for it, but want to run it inside a terminal, so I can practice Python code inside my IDE like VS code.
If you have a reasonably new version of Python you need to run py.exe (or just py) instead of python.exe, because the real python executable (python.exe) is not in your path environment variable by default, whereas py.exe is in the /windows directory and thus automatically in path.
So to run your script type py yourscript.py from the directory of your script. If you just run py it will start the interactive interpreter.
Look e.g. at https://www.python-course.eu/python3_execute_script.php
Under the title Start a Python script (almost at the beginning)
there is an instruction. Just write from the command prompt:
python,
a space,
and the filename containing your script, with .py extension.
As in each command, press Enter and your script will be executed.
Of course, I assume that you have installed Python on your computer.
If this is not the case, start from installing it.
I have quite a new Python version installed (3.6.5) and start Python
writing just python. Writing just py also does the job, but (IMHO)
it is not necessary.
If you want to start Python interpreter and play around with some
Python commands, write just python and Enter. The interpreter will
start and display its own command prompt (>>>).

Python script that can run from OSX Finder and Windows Explorer

I have a bunch of useful Python3 scripts that I use in different OS environments. They run fine when called from the terminal but I want to run them from both Windows Explorer and OSX Finder. In Windows, that is not an issue as the *.py files get associated with the installed interpreter.
For OSX, I am having some problems. I tried using the shebang line.
#!/usr/bin/python3
But Finder doesn't recognize it as a script. How do I make Finder recognize *.py files as scripts? I am interested in maximum portability with as few OS modifications as possible.
Also I explicitly specified the interpreter location above. It will be in different locations for different OSes. Is there a way to make it multi-OS so that if it doesn't find it in one location, it will try another? Say if I want to run it from one of Ubuntu's GUI File Manager.
EDIT: Here's more info on my use case. I want to go to a new computer and git clone my repo of Python3 scripts and be able to run them from within the GUI file manager with minimal changes to the new computer.
To make the script more portable use #!/usr/bin/env python3 as shebang. Side effect will be that it will run the first python3 interpreter it finds in the $PATH (Unix StackExchange question on that subject)
To run the script from the finder try to chmod the file. i.e.
chmod +x FILE
Found another possibility here: Link to the Blog
Make this the first line of your Python script "#!/usr/bin/env python"
Change the extension of the script file to ".command" i.e. my_python_script.command
In Terminal make the Python script file executable by running "chmod +x my_python_script.command"
Now when you double click the Python script in Finder it will open a terminal window and run.
Alternative I found would be this but it needs system modification: Superuser question

How do I run the python files from command shell/command prompt?

Really frustrated with this as one thing works at one time. Sometimes import filename.py works. But in the tutorials all I see is python filename.py. But when I try to give that, I am facing an error like invalid syntax.
I have edited all the environment variables and I have C:\Python27 folder in the same location. To be able to run the files using python filename.py what are the conditions that must be met? What should be the current working directory? Should the .py files be there in the same working directory?
import name is a python keyword for use within a python script and/or the python interactive interpreter (repl).
python filename.py is a shell command for use at a command prompt or within a shell script to run the python interpreter on a given python script file.
The working directory does not matter other than for whether the file listed in python filename.py can be found.
So for python filename.py to work you must be in the same directory as filename.py but you could just as readily use python c:\users\user\some\other\path\filename.py in which case the current directory isn't used to find the file.
If you get python syntax errors from attempting to run python on a python file that's a python error in the code and you will need to look at the python file to see what the error is.
Just to be clear, typing python filename.py only works from the Terminal (i.e. cmd.exe, Windows PowerShell, the "Terminal" application on a Linux kernel, etc.), not from the Python interpreter (i.e. python.exe), and only works if you have used the cd command to change into the directory in which the file is saved (so that the terminal knows where to look for filename.py). import filename can be used from the Python interpreter, but is not the ideal method as it creates a compiled version of filename.py and can only be used once (you would have to restart the interpreter to do so again). I'm not sure whether this works in the official Python distribution available from the Python website, but at least in the Anaconda distribution, you can run a file from the Python interpreter using runfile("C:/Users/CurrentUser/Subfolder/filename.py").

Starting Python tutorials but have some beginner questions

Hello I am attempting to learn Python using django in powershell on windows to create a simple CRUD app but have some questions regarding the setup and usage.
I have setup the path environment variable to include paths to a folder that includes test .py scripts that I thought allowed me to run directly from a c:\ in powershell but when I try to run the samples using:
c:\> python test1.py
c:\> python .\test1.py
It does not work and I get:
c:\Python27\python.exe: can't open file '.\test1.py':[Errno 2] No such file or directory
but if I CD to path first and run it works fine and I get the output in the powershell window itself:
c:\> cd c:\Python27\Scripts
c:\Python27\Scripts> python test1.py
In addition if I just type the name of the script from the root c:\ prompt such as:
c:\> c:\Python27\Scripts\test1.py
c:\> test1.py
it runs the script by opening in a dos box that that closes immediately since it ran the script and finished.
What is the difference between these methods and is there an issue running scripts one way or the other?
The issue here is that in the first case:
c:\> python test1.py
c:\> python .\test1.py
You're running python itself, and then the file is an argument to the interpreter. So python will be affected by PATH, but the file is relative. Both of these file paths mean to look for it in the same directory, which is the root of C:. Since the file isn't located there, the error results.
The second case:
c:\> c:\Python27\Scripts\test1.py
c:\> test1.py
It's the file itself being run as an "executable", factoring in PATH. On Unix, this would use the shebang and invoke the shell in pretty much the same way, however that's not the case on Windows. Instead, it's basically running whatever is associated with the .py extension at the OS level, which is likely to invoke a new Python interpreter in a new Window, hence the behavior where it disappears after it's done.
On Windows, since there's no shebang, I generally prefer to run the python interpreter directly and pass the script as an argument. I would generally switch to the directory the script is located in first. This way it's more shell-centric, and, I think, straightforward.
You can run pushd . to save the cwd, and then after popd to go back, optionally. That would work in .cmd files and similar, too.
Are you sure test1.py is in C: and not in c:\Python27\Scripts

Run a python script in windows

I have always used a mac to write and run python scripts. However, I have a bunch of files on a PC that I need to run a script on. I have never really used a PC and don't know how to run the script.
If I go to the command program on the PC and type in python, nothing happens. How do I find where the python path is in order to get into the python prompt? Also, once I am in the prompt, is the importing of modules the same as in a Unix system?
Python isn't added to the system environment's PATH variable by default on windows. You have to either add the path to the directory containing the Python.exe file to the PATH variable, or call python explicitly.
This issue has been addressed in the Python documentation:
Python Documentation: # How to run a Python program under windows
Assuming Python is installed, it is usually placed in a folder prefixed with "Python" and the major/minor version. E.g. C:\Python26

Categories