I recently installed Python 2.7.6 for 64 bit Windows7. I am trying to run few commands like cd, pwd etc and it is not running either on Windows Command Prompt, or Python Commnad Line. and i am getting the follwoing error:
>>cd Desktop
File "<stdin>", line 1
cd Desktop
^
I am also not able to execute any .py codes
(I cannot post image as i dont have enough reputations)
The Python interpreter doesn't accept shell/batch commands, but only valid python code. What you are trying to do in your example would translate like this in python:
import os
os.chdir("Desktop")
What you did is start the Python interpreter in interactive mode (or maybe did you start IDLE). To run a python script saved in a file from the command line, try
C:\Python33\python.exe your_script.py
(change the path to python.exe according to your installation). If you installed Python fully, you should be able to launch scripts by double clicking on .py files.
If you use IDLE, see the official documentation. You can open a script from the menu and run it.
(Note that pwd is not a valid command for the Windows shell either (use cd without argument instead.)
Related
I am trying to run Python from VS Code. I have already activated python through the terminal. However, in the terminal selector in the lower right of the screen, I cannot find Python terminal option there:
I wish to ask how could I make Python terminal visible here so that the next time I wish to switch terminal to Python, I do not have to load a .py file but could just selecting Python terminal directly. Thank you.
Python terminals aren't really a thing. If you want to run a .py file, do $ python [filename].py or $ python3 [filename].py. If you want to just run commands, open a Python shell by doing $ python or $ python3
There is no Python terminal. If you are asking about loading Python shell you just have to type python into your terminal to load the Python shell
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 (>>>).
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.
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").
Is there a way to control what version of python is run when double clicking on a py file? From the command line and in environments such as eclipse I can control what version is run. But from double clicking I am not sure.
I have 2.6 and 2.7 installed. 2.6 is for some application specific stuff and I want to make 2.7 the default. I have added "C:\Python27" to the PATH environment variable and that works well at the command line. C:\path\to\some\file>python someFile.py will run the file in 2.7. But if I double click the same file from explorer it runs 2.6. How to get it to run 2.7?
On Windows, you have to modify the file associations, for example via Right Click → Open with ... → Choose default program or the Control Panel's Folder Settings. You can choose between multiple python installations by navigating to the python.exe you want via the Browse button:
Alternatively, you can change the association in a command shell by typing
ftype Python.File="C:\Python27\python.exe" "%1" %*
Note that this requires administrator rights. If UAC is enabled on your machine, right click cmd in the start menu and select Run as administrator.
On freedesktop.org-compatible desktops, you can configure the association with xdg-mime.
On debian-based distributions, you can change the default python with update-alternatives. On all systems, you can also symlink the python in your path to the correct implementation, like this:
$ sudo ln -sf python2.7 /usr/bin/python
If the file is marked executable, it can also be executed directly from the command line or GUI if it starts with #! and the name of the interpreter:
#!/usr/bin/env python
To choose a specific Python version just for your program, you can start your Python program with one of the following lines:
#!/usr/bin/env python2.7
#!/usr/bin/python2.7
OK I have found the Python Launcher, which does exactly what I am after. Download can be found here. Installing this gave me the option for "Python Launcher for Windows (GUI)" when changing the file association via the right click menu.
Adding the shebang line
#!/usr/bin/python2.7
forces the script to run in 2.7.
This works great as I can control what version of python is running and users never need to know. No need for bat files, or dragging onto shortcuts etc. Nice and clean, and most importantly, no room for user error.
You can use ASSOC and FTYPE
assoc .py=pyfile
ftype pyfile=c:\Python27\python.exe %1
You will find answer in answers to the following questions:
How to execute python scripts in windows?
Using older Python 2.x while having both Python 2.x and 3.x installed on Windows
Temporary file association for single cmd.exe session
How to write a batch file showing path to executable and version of Python handling Python scripts on Windows?