NameError: name 'ls' is not defined - python

I am running Windows 7 and just installed Python 2.7.4 and all I get are SyntaxError:'s. For example when I type:
>>>ls
I get
Traceback (most recent call last):
file "<stdin>", line 1, in <module>
NameError: name 'ls' is not defined.
I am new to Python and have no idea what the problem is.
Math calculations work as well as code seems to work. It's just commands like cd, ls, sudo apt-get update (and upgrade). Any guidance would be appreciated.

I think it's unfortunate that you're being downvoted so heavily here. If you're new to programming, this can be an honest mistake to make since a lot of tutorials assume that you're already familiar with the command line.
The main problem you have (as others have pointed out) is distinguishing between the operating system shell (which is also known as the console, terminal, command line, or command prompt) and the Python shell.
The operating system shell/command prompt is one of the fundamental ways you can interact with your computer. When you open the command prompt, you run arbitrary programs by typing in their name, run commands such as ls, sudo, cd, and apt-get.
If you type in python, for example, it'll cause the command prompt to start the Python shell. From there, you can run all of the Python commands, as usual. However, this is an entirely separate program with an entirely separate set of rules. The things you can do in the command prompt will not work in Python, and vice-versa.
Now, another point of confusion is distinguishing between the Windows command prompt and the Linux command prompt. You can install and run Python in both, but certain commands (in particular, sudo and apt-get) are available only on Linux operating systems. Unfortunately, the rules for using the Windows command prompt and the Linux command prompt differ somewhat, meaning that some instructions you find will be applicable only in one kind of operating system.
So no wonder you're confused! You're trying to learn about Python, but a good number of tutorials/articles will mention this second, entirely different system with hardly a word of introduction, and expect you to already know how to use it.
If you're curious about learning more about using the command line, here's a good crash course you can read. It'll teach you what you need to know for all 3 major operating systems (Windows, Linux, and Mac) and will hopefully make what you read online more clear.

I see from your profile you are learning about Raspberry Pi. A Raspberry Pi can use Debian (or Ubuntu, which is based on Debian) as the Linux distribution. The commands you are trying in Python are shell commands to run on that operating system.
Python is another command you can run on Raspberry Pi, but that doesn't mean that installing Python on Windows gives you those other commands on Windows too. You have two entirely different things confused here.

it's just commands like cd, ls, sudo apt-get update (and upgrade). Any
guidance would be appreciated
Python doesn't support those commands. Those are operating system level commands, not python level commands.

The Root of my problem was that I was not asking the right question. I was trying to run scripts in the interactive-mode and entering 'ls' and 'dir' to see where I was in the directory. With everyone's guidance I finally found the part in the Docs about Executable Python Scripts that said Windows automatically associates the 'py' extension with python.exe.
here. Then it all came together in my head. I had associated the 'py' extension with my editor instead of python and that I should have been at the Windows command prompt.
Thanks for everyone's help and quick responses.

The answers above are correct, but they don't help you with your issue!
One solution that will work in Python is:
%ls

Related

Running Python through Powershell or Git Bash

I will preface this with, I know questions like this have been asked, but I feel as I can't find my answer.
My question is this: I downloaded Python about 2 months ago to begin learning it. I just recently starting trying to use the terminal more and would like to move forward using it a lot for commits and package installs.
Unfortunately I am having a very hard time getting Python to run properly through either Git Bash or Windows Powershell. I think i may have moved files around too much before even starting with using the terminal and now I'm not sure what's correct.
Should I uninstall and re-install?
I am on Windows 10, any advice onhow to organize my files properly so everything can run correctly. I would also like to be able to open my text editor from the terminal, PyCharm or Atom.
Thanks in advance!!
Try first to simplified your PATH (as I did here) in a CMD session.
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
set GIT_HOME=C:\Path\to\Git
set PATH=%GIT_HOME%;%GIT_HOME%\bin;%GIT_HOME%\usr\bin;%PATH%
SET PATH=C:\Users\Aerovistae\AppData\Local\Programs\Python\Python36-32;%PATH%
Once you have the right path, you can replicate it in your user environment variables.
(replace C:\Users\Aerovistae\AppData\Local\Programs\Python\Python36-32 by your own Python2 installation path)
Then type bash and check python does work.

Downloading python 3 on windows

I am currently trying to figure out how to set up using python 3 on my machine (Windows 10 pro 64-bit), but I keep getting stuck.
I used the Python 3.6 downloader to install python, but whenever I try to use Command Prompt it keeps saying "'python' is not recognized as an internal or external command, operable program or batch file" as if I have not yet installed it.
Unlike answers to previous questions, I have already added ";C:\Python36" to my Path environment variable, so what am I doing wrong?
I am relatively new to python, but know how to use it on my Mac, so please let me know if I'm just fundamentally confused about something.
In environmental variables under path, add your python path... you said you already so please ensure is their comma separation between previous path..
And once added save environment variables tab. And close all command prompt then open it.
Then only command prompt will refresh with your python config..
Main thing, if you enter python which mean python 2.
For python3 type, python3 then it should work
Why are you using command prompt? I just use the python shell that comes with IDLE. It’s much simpler.
If you have to use command prompt for some reason, you’re problem is probably that you need to type in python3. Plain python is what you use for using Python 2 in the command prompt.
Thanks everyone, I ended up uninstalling and then re-downloading python, and selecting the button that says "add to environment variables." Previously, I typed the addition to Path myself, so I thought it might make a difference if I included it in the installation process instead. Then, I completely restarted my computer rather than just Command Prompt itself. I'm not sure which of these two things did it, but it works now!

How would I get a Unix command prompt in Windows / USB key?

So I'm a teacher at a school, and I don't have the ability to install very many things. We currently use Windows 7. I can't change any environment variables.
I have installed the following software:
Ruby 2.2.4 with Command Line Prompt
Python, including IDLE
Is there any way to have a portable version? I can put them onto USB sticks.
I only want to run the most basic unix commands:
Like getting the word count of a file:
wc file.txt
Or displaying the contents of a file:
file file.txt
Literally, very basic commands like that. It's part of an exercise for my students.
Find a portable Cygwin(https://github.com/GathSystems/CygwinPortable for example). I've used it exactly like you - from usb stick.
Busybox on windows should work just fine for what you want to do.
You don't need to install anything. Just downloading the stand-alone executable should be enough.
Executing busybox.exe bash would bring you a shell which knows wc, file and cat among many other commands.

How to use Python3.2 with Dreampie? [UBUNTU]

I have a question.
I try to learn Python and of all programmer's tools I found Dreampie the most suitable. But, my dreampie always automatically sets Python 2.7. I found this topic and following the instructions, I changed both subp_main.py and subp_main.pyc (based on the first). Yet, it's still Python 2.7.
Telling truth, I suppose there should be some way to choose between the two versions, but I did not found anything in the Net or in program options. According to this topic writing "dreampie python3" inside the program should work, but dreampie inform me that the syntax is invalid (?).
[I quess the solution is very easy, but none of searchable things would help...]
You need to open a Terminal window (or Run dialog Alt+F2) and type in the command as follows and press Enter:
dreampie python3
It's not python syntax but shell syntax so you are running the dreampie application with python3 as argument.

How to install python 3.2.3 on Windows 7 enterprise

although I have been using python a long time very easily in a Linux environment, I have tremendous trouble to even install it correctly in a windows environment. I hope this is a question to be asked here, as it is not directly a programming question.
Especially, I have the following problems:
When on the command line, python is not a recognized command. Do I have to set the Windows path manually myself? If so, how to do that?
When starting a python script, should this be done with python.exe or pythonw.exe? What is the difference?
I also tried to install ipython several times, it never got installed (even after following the starting ipythonenter link description here thread.
When starting a script with python.exe, a window pops up and closes immediately. I saw some hints in putting in a readline command, which is of no help if there is a syntax error in the script. So how to be able to keep the window open, or how to run the command on the cmd.exe?
Thank you for any help on these items.
Alex
1) Look here: www.computerhope.com/issues/ch000549.htm
2) It has already been answered, always try to use search before asking question:
pythonw.exe or python.exe?
4) When using cmd.exe just navigate to your script folder using dir for changing directories and C:,D:,etc. for changing drives. Then run script by typing just the script name. When installed correctly, Python automatically launches .py scripts with python, so you don't have to write 'python' before script name. When run in cmd, window will stay open. If you want it to stay open even when launching script with double-click, use function waiting for user input, see here How to keep a Python script output window open?
You might want to use Python3.3, there is a new launcher for Python scripts in it. By that, you can start Python scripts with py <scriptname> which has the benefit of being installed in your path (C:\Windows\system32) and you can use a shebang to tell whether the script is for Python2 or Python3.
Also
In addition to the launcher, the Windows installer now includes an
option to add the newly installed Python to the system PATH
(contributed by Brian Curtin in issue 3561).

Categories