EDIT-4
I've gotten my sitecustomize.py to execute, but it tosses up an error. Here's the code for it.
The error is:
Error in sitecustomize; set PYTHONVERBOSE for traceback:
RuntimeError: maximum recursion depth exceeded while calling a Python object
I'm not terribly advanced with Python yet, so I figured I'd comment out only the lines I did not think Iwould need. No encoding issues are showing up, so I just commented out lines 23-104, but that didn't help either.
EDIT-3
I also happened to have 2.5.1 installed, so I compiled another script with that.
print 'This will test carriage returns on Windows with PyDev on Eclipse Helios'
print'Type something:',
test = raw_input()
print('You entered the following ascii values:')
for c in test:
print(str(ord(c)))
This ran fine, and resulted in
This will test carriage returns on Windows with PyDev on Eclipse Helios
Type something: g
You entered the following ascii values:
103
So this is possibly a Python3 thing only? I know it's not the interpreter, because I'm able to run it in command prompt just fine. What gives?
EDIT-2
Just tested with Helios, still having the same problem. Here's my test program:
print('This will test carriage returns on Windows with PyDev on Eclipse Helios.')
print('Type something:', end='')
test = input()
print('You entered the following ascii values:')
for c in test:
print(str(ord(c)))
And here's the output when I type 'g' and press Enter:
This will test carriage returns on Windows with PyDev on Eclipse Helios.
Type something:g
You entered the following ascii values:
103
13
In the grand scheme of things, it's a small issue. I could use input().rstrip() and it works. But the workaround shouldn't even be necessary. I'm typing twice as much as I should need to in a language that I'm using because it's concise and pretty.
EDIT-1
This is Eclipse 3.5. Unfortunately that's the latest version that's been approved for use at work. I'm going to try 3.6 at home to see if that's any different, but I wouldn't be able to use it anyway.
(original question)
I've been learning some basic Python, and decided to go with PyDev since it supported Python 3 as well as having all the nice code snippet and auto complete features.
However, I'm running into that darned carriage return issue on Windows.
My searches always lead me back to this mailing list:
http://www.mail-archive.com/python-list#python.org/msg269758.html
So I have grabbed the sitecustomize.py file, tried to include it in the Python path for my configured interpreter, as well as my project, but to no avail.
Has anybody else managed to work through this? Or maybe knows how to get the new sitecustomize.py to actually execute so it can override input() and raw_input()?
I know I could always make a short module with my own replacement input() function, but I'd really like to fix the problem at its root. Aptana acknowledges the issue ( http://pydev.org/faq.html#why_raw_input_input_does_not_work_correctly ) but offers no solution. Thanks in advance for your help.
Figured out a hack to make it work locally to my Python installation. In \Lib\site-packages\ make a script called "sitecustomize.py", and put this code in it:
original_input = builtins.input
def input(prompt=''):
return original_input(prompt).rstrip('\r')
input.__doc__ = original_input.__doc__
builtins.input = input
I don't know anything about the side effects of this, or what sort of error checking I should be doing, but it works if you're using PyDev on Windows to write scripts with Python3.
Found out some more things about sitecustomize.py and how it relates to site.py.
I don't know how to add my own sitecustomize.py to PYTHONPATH for execution only in a PyDev project, so I just stuck it in ${Python31dir}\Libs\site-packages. The module runs now, but generates errors.
Related
I have a python script that requires Python 3.8 or better to support the walrus operator and other Python3 operations. I want to test the version and output a "nice" message if the minimum version is not detected, however, I am getting the following syntax checking error if I am on Python2 and the script will not run to give the "nice" message.
File "./te_add_for_wcs.py", line 743
if (cert_count := apiResponse.get("X-Total-Count","NO_COUNT")) == 'NO_COUNT':
^
SyntaxError: invalid syntax
Is there a way to get around this, or am I out of luck when users of Python2 attempt to use my script and would need to figure out the error means wrong version?
Trying to keep this to just one script file, as I can think of ways use multiple scripts that call each other to take care of prerequisites.
I appreciate all the "comment" answers!
#MattDMo answer is what I will need to do, as I have no interest a ton of extra work (as indicated by #chepner), because #Kraigolas is absolutely correct - version 2 should not be used by anyone in production, or only used in isolated environments.
Developers like myself should assume that people will be using version 3 of Python and I should be documenting it well that scripts will only support Python3 and have logic that detects the minimum version of Python3 that is required.
Thank you again!
The problem is that := is a syntax error, raised before any portion of the script can execute. You could "hide" the use of := in a module that is imported conditionally, based on a version check.
if sys.version_info.major < 3 or sys.version_info.minor < 8:
sys.exit("Script requires Python 3.8 or later")
else:
import module.with_function_using_assignment_expression
Rather than an error, though, I would just hold off using := in the code, but adding a deprecation warning to the script to let users know that a future version of the script (with :=) will require Python 3.8 or later.
import warnings
# Or maybe DecprecationWarning; I'm not entirely clear on the
# distinction between the two.
warnings.warn("This script will require Python 3.8 or later in the near future",
warnings.FutureWarning)
...
# TODO: use an assignment expression to define cert_count directly
# in the if condition.
cert_count = apiResponse.get("X-Total-Count", "NO_COUNT")
if cert_count == "NO_COUNT":
...
After a suitable waiting period, you can go ahead and use :=, and let the users accept the consequences of not upgrading to Python 3.8.
While I understand wanting to keep this in one file, for completeness, here's an easy 2-file solution:
wrapper_main.py
import sys
if sys.version_info.major < 3: # replace with your exact version requirements
print("Please upgrade to python3!")
sys.exit(0)
import main
main.main()
main.py
def main():
a := 1
print("Entering main!")
Tested on python 2.7 that this runs and exits without syntax error.
I am new to PyCharm and I have 'Process finished with exit code 0' instead of getting (683, 11) as a result (please see attachment), could you guys help me out please? Much appreciate it!
That is good news! It means that there is no error with your code. You have run it right through and there is nothing wrong with it. Pycharm returns 0 when it has found no errors (plus any output you give it) and returns 1 as well as an error message when it encounters errors.
Editors and scripts do not behave like the interactive terminal, when you run a function it does not automatically show the the result. You need to actually tell it to do it yourself.
Generally you just print the results.
If you use print(data.shape) it should return what you expect with the success message Process finished with exit code 0.
exit code 0 means you code run with no error.
Let's give a error code for example(clearly in the below image): in below code, the variable lst is an empty list,
but we get the 5 member in it(which not exists), so the program throws IndexError, and exit 1 which means there is error with the code.
You can also define exit code for analysis, for example:
ERROR_USERNAME, ERROR_PASSWORD, RIGHT_CODE = 683, 11, 0
right_name, right_password = 'xy', 'xy'
name, password = 'xy', 'wrong_password'
if name != right_name:
exit(ERROR_USERNAME)
if password != right_password:
exit(ERROR_PASSWORD)
exit(RIGHT_CODE)
I would recommend you to read up onexit codes.
exit 0 means no error.
exit 1 means there is some error in your code.
This is not pyCharm or python specific. This is a very common practice in most of the programming languages. Where exit 0 means the successful execution of the program and a non zero exit code indicates an error.
Almost all the program(C++/python/java..) return 0 if it runs successful.That isn't specific to pycharm or python.
In program there is no need to invoke exit function explicitly when it runs success it invoke exit(0) by default, invoke exit(not_zero_num) when runs failed.
You can also invoke exit function with different code(num) for analysis.
You can also see https://en.wikipedia.org/wiki/Exit_(system_call) for more details.
What worked for me when this happened was to go to
Run --> Edit Configurations --> Execution --> check the box Run with
Python Console (which was unchecked).
This means that the compilation was successful (no errors). PyCharm and command prompt (Windows OS), terminal (Ubuntu) don't work the same way. PyCharm is an editor and if you want to print something, you explicitly have to write the print statement:
print(whatever_you_want_to_print)
In your case,
print(data.shape)
I think there's no problem in your code and you could find your print results (and other outputs) in the tab 5: Debug rather than 4: Run.
I just ran into this, but couldn't even run a simple print('hello world') function.
Turns out Comodo's Firewall was stopping the script from printing. This is a pretty easy fix by deleting Python out of the Settings > Advanced > Script Analysis portion of Comodo.
Good Luck
I had same problem with yours. And I finally solve it
I see you are trying to run code "Kaggle - BreastCancer.py"
but your pycharm try to run "Breast.py" instead of your code.
(I think Breast.py only contains functions so pycharm can run without showing any result)
Check on tab [Run] which code you are trying to run.
Your starting the program's run from a different file than you have open there. In Run (alt+shift+F10), set the python file you would like to run or debug.
I new to python. I use Python 3.3 in Eclipse Kepler.
This is my code snippet:
f = Fibonacci(0,1)
for r in f.series():
if r > 100: break
print(r, end=' ')
At the line print(r, end = ''), eclipse reports a syntax
error - Syntax error while detecting tuple. However, the
program runs perfectly.
Why does this happen and how do I fix the error?
You need to specify the correct Grammar Version in Eclipse. See here: print function in Python3
Is Grammar Version 3.3 in your setup? Steps - Project > Properties > Python Interpreter/Grammar. You might have to restart Eclipse to see the changes.
I know this is an old post, but for those of you with similar issues in 2018 and later, CodeMix for Eclipse is a plugin you can install to boost your python development with almost none configuration needed. You get Content Assist, Validation, shortcut commands for stuff from refactoring to formatting, etc. More informaiton here
In python 2.x print is a built-in keyword, not a function as in python 3.x and is used like this :
>>> print "hello", "world"
hello world
Therefore, Python assumes that (r, end= '') is a tuple containing two values, that you are trying to print.
You can probably configure eclipse to use python 3.x syntax. Check this if you are using PyDev.
Change the run path in eclipse to python 3.x rather than its current setting which is probably set to python 2.x
I think it's possible you're actually using Python 2. Do this and report your results:
import sys
print(sys.version)
EDIT:
Actually, what you're reporting appears to be an open bug on pydev for Eclipse:
http://sourceforge.net/p/pydev/bugs/913/
I wrote a small text adventure in Python 3.2.2 and sent the .py file to a friend who is using a mac.
He ran the code after downloading the latest python for snow leopard and it ran alright until the line in the code: var = input("press any key to continue"). After that it just stayed at that line not producing any errors or doing much of anything, except that nothing happened when he typed anything. The characters he typed showed up at the prompt, but the program never moved forward.
I then froze the program using cx_freeze and sent it to my sister who is running windows xp (as am I) and she had the same problem. The game loaded up fine until that line.
What am I doing wrong?
Thanks!
They are probably using python 2.x, and need to press the return key. input expects a line of input, not a single character. The input function fundamentally changed between 2.x and 3.x, and the behavior you see is consistent with python 2.x.
For background on the change in behavior, see PEP 3111
I was able to configure NetBeans for 2.6.1 by going to to the Python Platform Manager, creating a new platform, and pointing NetBeans at python.exe where I installed 2.6.1. However, when I follow the exact same steps for 3.0, I get an error in the NetBeans console that says "SyntaxError: invalid syntax".
If it matters, Python is installed in this format:
/Program Files
/Python
/2.6
python.exe and everything else
/3.0
python.exe and everything else
I'm wondering if anyone else has experienced this and what they did to correct the problem.
Yep- it's actually very easy. The scripts in the plugin use 'print' as a keyword which has been changed in Python 3; you just have to convert all 'print' statements in the console.py and platform_ info.py files under the 'python1' folder in your NetBeans installation directory to use parenthesis. For instance, in platform_info.py the first print line says:
print "platform.name="+ "Jython " + version
Change it to:
print("platform.name="+ "Jython " + version)
And do this for all print statements. Then go into the NetBeans and import your Python30 directory into the Python Platform Manager; it will work just fine.
I haven't run into any other issues yet, but there might be some other small syntax issues in the plugin; they should be very easy to fix.
It doesn't let me comment back here so I'll answer your comment in an post.
Yes, it will let you use Python 2.x as well; the 'print' method was both a keyword and function prior to Python 3, so the parenthesis were optional. As on 3 they are required, so this change is backwards compatible.
There are some issues with debugging, btw- I'll let you all know when I successfully figure out what has to be updated here.
Thank you Ben Flynn for the solution to integrate python30 with netbeans 6.71
However, this piece of code :
def fib(n): # write Fibonacci series up to n
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while b < n:
print (b, end=' ')
a, b = b, a+b
fib(2000)
Which is an example code from a help site, runs with out error from the IDE,
but the editor complains:
Internal parser error
"no viable alternative at input'=' "
Which suggests it is parsing against python2.5.1
Starting at version 3.0, the print statement has to be written as a function...
your
print (b, end=' ')
becomes
print("end= ", b)