I tried setting up Python on my computer, but some of the steps in the article below differed from the way my PC behaves.
https://www.codecademy.com/articles/setup-python (I encountered difficulties under the ‘Running Python’ section)
I have the latest version of Python, and I have notepad++. However, when I tried searching ‘cmd’ on my computer, only command prompt showed up. I tried using this, and I was able launch Python, but it still won’t let me use a file by entering ‘
python name.py’. I’m assuming that I need to find the location of the file, but I don’t know the language to get to the file location. It says something about a ‘cd’ command, but I don’t know if I’ll have enough time to learn that before the trial ends. Help’s appreciated!
I’m seriously lost because other posts I have found say to
import os and
os.chdir(path), but I can’t make it work.
cd c:\users\username\Documents should get you to your personal Documents directory, if you put the script there, it will work. Do replace username with your actual username
or you need an text-editor with plugin (atom + plugin) or IDE (spyder) to run your scripts more easily if you can’t figure the command line out
File "<stdin>", line 1
SyntaxError: invalid syntax
This error pops up, and
C:\Users\myname\Documents does exist.
the change directory (
cd) of for the command line (cmd), not python. We need to tell the command prompt where the python script is located it needs to execute, which is why we navigate directory in the command line, or provide a path to the python file/script.
Thanks! That worked perfectly. So, in general, I need to be in regular command prompt to switch between directories. Although it works for command prompt, I’m curious on how to do the same in the program called ‘Python (command line)’ that came with the installation. It seems exactly like command prompt but it’s in Python by default. I tried
quit()ing, but that closed the whole window, not just Python.
Also, is there any point in typing
python into command prompt, anyway? I could run my .py file perfectly fine while still in the default command prompt. If that’s the case, it only seems useful to use
python when typing directly into command prompt or to access those help/copyright/module files.
if you have an editor/IDE that can execute script for you, then not.
Are you referring to the IDLE (integrated development and learning environment)? You can google what it is, but you can recognize it by the
>>> at the beginning of command prompt
the IDLE is certainly useful if you need to test something small
the programming language is named python, but the program that interpreters the code is also named python (its what you run in your command line), running
python program without any arguments will open IDLE. While
python scriptname.py will execute scriptname.py
typical usage is to execute python and provide a path to your code
…and that’s pretty much all there is. how you go about telling your os to do this is up to you and doesn’t have anything to do with python