I have Spyder 2.3.8 that I installed with Anaconda.
Python version is 2.7.11, conda version is 4.0.5.
I have found that some types of code make the Spyder editor stop responding. One example is the line:
x = b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'
I assume this is a valid python code. I am relatively new to Python, so please correct me if I am wrong here. I tried in on a terminal on the Python shell and it works.
Another very strange example is
png = 'oxy.png'
f = open(png, 'rb')
f.read(10) # That executes without any problems
f.close()
f = open(png, 'rb')
x = f.read(10) # this line makes Spyder freeze!
f.close()
The only difference here is that I assign f.read() to a variable, and that makes Spyder stop responding!
I just updated Spyder and all other anaconda packages. The error was happening before and it still happens. Any idea of what I could be doing wrong, or what could I try?
EDIT: There was this other part of the question which has been explained already (thanks!)
I also tried to run the following example I found on the internet:
name = input("What is your name? ")
print("Nice to meet you " + name + "!")
This example gives me an exception. Here is the full output:
name = input("What's your name? ")
print("Nice to meet you " + name + "!")
What is your name? sininho
Traceback (most recent call last):
File "<ipython-input-1-e82cc0e3f7a3>", line 1, in <module>
name = input("What's your name? ")
File "/scr/elbe9/pauline/physio/program/python_libraries/anaconda2/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 165, in <lambda>
builtin_mod.input = lambda prompt='': eval(self.raw_input(prompt))
File "<string>", line 1, in <module>
NameError: name 'sininho' is not defined
In Python 2, the input() function executes the code you type in. To get q string, use raw_input() function which works the same way.
Related
Learning python as a beginner. I wanted to copy my code into CMD but it won't work. Here is code
calculation_to_units = 24
name_of_unit = "hours"
def days_to_units(number_of_days):
if number_of_days > 0:
return f"{number_of_days} days are {number_of_days * calculation_to_units} {name_of_unit}"
else:
return "Liczba dni musi być dodatnia :)"
user_input = input("Hello user, enter amount of days you want to calculate to hours\n")
user_input_number = int(user_input)
calculated_value = days_to_units(user_input_number)
print(calculated_value)
despite the fact that it works in Pycharm. I already checked paths. I am not able to solve this problem. When I type in python3 test.py it also says C:\Users\Borys\AppData\Local\Programs\Python\Python310\python.exe: can't open file 'C:\Users\Borys\test.py': [Errno 2] No such file or directory
Also recieved this message "unable to initialize device prn in python"
My internet connection is so bad that it took me 10 minutes to sign up on stack overflow. Additionaly my english knowledge is too small for complex programming explainations.
It can be difficult to paste code that calls input() into a python shell. Both the shell and the input() function read stdin. As soon as python reads the line with input(), it calls input() and that consumes the next line on stdin. In your case, that was a python code line intended to set a variable. That line was consumbed by input and was not read or executed by python. So you got a "variable not defined" error. But you would also have gotten another error because that line was also not the stuff you wanted to input.
Suppose you had the script
val = input("Input value: ")
i_val = int(val)
print(i_val)
And pasted it into the python shell
>>> val = input("Input value: ")
Input value: i_val = int(val)
>>> print(i_val)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i_val' is not defined. Did you mean: 'eval'?
The line i_val = int(val) was assigned to val - it was not interpreted by the shell. There would be an ">>> " if it did.
When running python in netbeans, when I type in input, how and where do I take in input. eg
name = input("enter your name")
print("hello",name)
When I run this I get the output on the left in the shell but when I try and input anything in that window I get
Traceback (most recent call last):
File "C:\Users\Rahul\Documents\NetBeansProjects\NewPythonProject\src\newpythonproject.py", line 18, in <module>
name = input("enter your name")
File "<string>", line 1, in <module>
NameError: name 'hi' is not defined
I am using netbeans 8.2.
thanks!
When you use input() in Python 2, it performs and eval() on the user input.
To get the exact string the user typed, you have to use raw_input() to get exactly what the user typed.
Also in Python 2.7, the print is a statement rather than a function. You can lose the parentheses.
your code will look like,
name = raw_input("enter your name")
print "hello", name
If You use python version 2+
name = raw_input("enter your name")
print("hello {}".format(name))
If You use python version 3+
name = input("enter your name")
print("hello {}".format(name))
I have a script for sending emails using SMTP_SSL. The code is working fine in PyCharm but in the terminal I get an error.
This is the code:
import smtplib
s = smtplib.SMTP_SSL("smtp.googlemail.com:465")
mml=input("enter your email address :\n")
str(mml)
passr=input("enter your pass:\n")
str(passr)
s.login(mml,passr)
em = input("please type the email you want to send :\n")
str(em)
a = input("please type the message:\n")
str(a)
s.sendmail(mml,em,a)
print("\nEmail Was Sent..:)")
When I run this in my terminal its giving this after i enter the email:
enter your email address :
mahmoud.wizzo#gmail.com
Traceback (most recent call last):
File "medo.py", line 3, in <module>
mml=input("enter your email address :\n")
File "<string>", line 1
mahmoud.wizzo#gmail.com
^
SyntaxError: invalid syntax
When I am trying to put the email between quotes, e.g. "mahmoud.wizzo#gmail.com" its working fine.
How can I run my script in the terminal?
I suspect you're running your script using Python 2 on the command line. The behaviour of input() changed in Python 3.
Try running python3 my_file.py instead of python my_file.py.
This is actually the way Python works. The problem you have is that input takes input from the user and then "executes" or "compiles" it so if you enter 4+9 in the input it will produce the number 13. Not the string "4+9". So try using raw_input() in Python 2. You can also use sys.stdin.readline() to get a string I'm any Python version. Don't forget import sys.
The only thing i have changed is downloaded pygame on my mac via the terminal.
When i type the follwing code -
name = input("what is your name?")
print ("Hello", name)
into my IDE i get this in the terminal -
what is your name? Traceback (most recent call last): File
"/Users/mikeyboy/PycharmProjects/untitled11/waht.py", line 1, in
name = input("what is your name?") File "", line 0
^ SyntaxError: unexpected EOF while parsing
Process finished with exit code 1
What's gone wrong this code worked perfectly a month ago?
I solved this by putting
name = raw_input("What is your name?")
instead of ...
name = input("What is your name?")
Anybody know why the latter worked before and now it doesn't?
I am trying to setup a program where when someone enters a command it will run that command which is a script in a sub folder called "lib".
Here is my code:
import os
while 1:
cmd = input(' >: ')
for file in os.listdir('lib'):
if file.endswith('.py'):
try:
os.system(str(cmd + '.py'))
except FileNotFoundError:
print('Command Not Found.')
I have a file: lib/new_user.py But when I try to run it I get this error:
Traceback (most recent call last):
File "C:/Users/Daniel/Desktop/Wasm/Exec.py", line 8, in <module>
exec(str(cmd + '.py'))
File "<string>", line 1, in <module>
NameError: name 'new_user' is not defined
Does anyone know a way around this? I would prefer if the script would be able to be executed under the same window so it doesn't open a completely new one up to run the code there. This may be a really Noob question but I have not been able to find anything on this.
Thanks,
Daniel Alexander
os.system(os.path.join('lib', cmd + '.py'))
You're invoking new_user.py but it is not in the current directory. You need to construct lib/new_user.py.
(I'm not sure what any of this has to do with windows.)
However, a better approach for executing Python code from Python is making them into modules and using import:
import importlib
cmd_module = importlib.import_module(cmd, 'lib')
cmd_module.execute()
(Assuming you have a function execute defined in lib/new_user.py)