Running commands from Powershell - python

I am a beginner in Python and am learning via online tutorials. On Study Drill #6 for example 15 on this page, I'm trying to open up the 'ex15_sample.txt' file that I have created and exists.
This is what it says to do:
Start 'python' to start the Python shell, and use 'open' from the prompt just like in this program. Notice how you can open files and run 'read' on them from within 'python'?
In the command prompt, I entered python to start the Python shell and have tried typing in the following, which threw me an error:
open(ex15_sample.txt)
The error I get is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ex15_sample' is not defined
ex15_sample.txt is a file that exists in the same folder. How am I supposed to use open or any other command from the prompt? I am running Python 2.7.8 on Powershell, by the way.

The first argument of open is a string containing the filename.
Just put file name in Quotation Mark:
f = open('ex15_sample.txt')

Related

Arbitrary strings in bash throw python errors?

Something in my setup of my shell causes arbitrary strings like "krmpfl" or "u45g5svtJ7" to create a Python error:
$> krmpfl
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I would expect bash (and not python!) to throw an error of the kind "Unknown command krmpfl. Did you mean...", but any non-recognized command is for some reason passed to python. I am confused.
Does anyone have an idea on how to debug this or how to move forward? I've tried type krmpfl but this (correctly) echoes bash: type: krmpfl: not found
My setup:
Win10 using Ubuntu 18.04 within WSL
ConEmu as a console
Bash-it
Python 3.8
Click (python package) installed to simplify creating commands
If your current shell function defines a function named command_not_found_handle, bash runs that for a non-existent command rather than immediately failing with a "command not found" error. In your case, that function exists and calls /usr/lib/command-not-found, which appears to be a Python script that tries to download (or at least suggest you download) a package with apt_pkg, but you don't have that Python module installed, which leads to the Python exception.

execute a python file from pycharm console

I have a python file and I want to execute it from PyCharm console.
I'm in the directory of the project (where there is my file)
I tried to follow the solution in this thread: Running a module from the pycharm console
but I got the following error:
from project_main_folder import *
home_scorer_macro.py
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-20-24aaf6f313c6>", line 1, in <module>
home_scorer_macro.py
NameError: name 'home_scorer_macro' is not defined
I tried directly without "import" to call execute the file, like:
python home_scorer_macro.py
but I got the following error as well:
File "<ipython-input-29-735c29c27d57>", line 1
python home_scorer_macro.py
^
SyntaxError: invalid syntax
You are confusing the console for the command prompt. The console allows you to type code and have it interpreted as you type line by line (or function by function). The command prompt allows you to say things like python home_scorer_macro.py. If you are trying to run the home_scorer_macro function from learning you can try:
from learning import home_scorer_macro
home_scorer_macro()
It is difficult to tell what you are trying to do without you providing any description of what you are trying to accomplish, though. You could also try something like
import os
os.system('"python home_scorer_macro.py"')
raw_input()
if you would like to combine commmand prompts and consoles. The os.system lets you run commands from within your python script
As well you can go to the Terminal tab and run python home_scorer_macro.py

Why does python script works differently in Spyder vs cmd prompt

I have the following script test.py:
import pathlib, os
path = "C:\\Windows"
pathparent = pathlib.Path("C:\\Windows").parent
if os.path.exists(pathparent):
print("path exists")
and when I execute it in Spyder IDE I get this:
path exists
when I run it from the command prompt (python test.py) I get this:
Traceback (most recent call last):
File "test.py", line 6, in <module>
if os.path.exists(pathparent):
File "C:\Anaconda3\lib\genericpath.py", line 19, in exists
os.stat(path)
TypeError: argument should be string, bytes or integer, not WindowsPath
Any idea why I get the different results?
Note: I know that wrapping pathparent in str() will make the if statement succeed, but what I want to know is why the the two environments yield different results.
os.path.exists() started accepting path objects in Python 3.6 and your problem is occuring in your cmd prompts as it is running Python 3.5, change it to 3.6 to fix your problem.

Import android command throws error while running the command on Python shell

I am trying to execute "import android" command on the python shell. It gives error like this :
>>> **import android**
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
import android
File ".\android.py", line 51
print result['error']
^
I'm having python 3.3.2 installed on the PC (windows 7) and have android.py (SL4A) available in the python directory on my system and the system path also contains python.
Plz help in resolving this error.
Print changed from a statement to a function on Python 3. And the module(android) you're trying to import is written for Python 2.
You should either use a Python 2 interpreter, or find(or make) that module compatible with Python 3.

'sleepymongoose' is not defined?

I am trying to set up sleepy.mongoose, a mongoDB REST interface using the instructions here.
I am using windows 8, :( ...anyway... I have python 3.3.2 installed and it is accessible from the cmd prompt. I also have pymongo installed, when i enter help('modules') to python, pymongo is on the list of available modules!
But when I try to run python httpd.py from the sleepy.mongoose dir (thanks karthikr), I get an error:
C:\Users\Brook\Desktop\sleepy.mongoose>python httpd.py
Traceback (most recent call last):
File "httpd.py", line 1, in <module>
sleepymongoose/httpd.py
NameError: name 'sleepymongoose' is not defined
Now I tried actually cding into the proper dir, but I get this other error:
C:\Users\Brook\Desktop\sleepy.mongoose\sleepymongoose>python httpd.py
File "httpd.py", line 221
print "\n================================="
^
SyntaxError: invalid syntax
I have had the same problem with Python 2.7 on Windows. Considering 'httpd.py' wich contains only:
sleepymongoose/httpd.py
You need this command line to start it:
python sleepymongoose/httpd.py
You are using python 3 to run python 2 code. The syntax for print amongst other things has changed. Use Python 2 or a server which supports python 3.

Categories