Setting up Python (using .py files and locating a directory)


#1

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.


#2

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


#3
 File "<stdin>", line 1
    cd C:\Users\myname\Documents
       ^
SyntaxError: invalid syntax

This error pops up, and C:\Users\myname\Documents does exist.


#4

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.


#5

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.


#6

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


#7

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