simple question: in python3 env, why can't run simple print? - python

print("hello", end="")
output:
[Running] python -u "/home/a/code/projects/project1.py"
File "/home/a/code/projects/project1.py", line 1
print("hello", end="")
^
SyntaxError: invalid syntax
settings.json
{
"python.linting.pylintEnabled": false,
"python.linting.enabled": true,
"python.linting.flake8Enabled": true,
"python.linting.pydocstyleEnabled": false,
"python.pythonPath": "/usr/bin/python3.6"
}
been looking up about why code is being run on older version of python in vscode... can't figure it out.

That's because you're using Python 2:
Python 2.7.16 (default, Oct 10 2019, 22:02:15)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello", end="")
File "<stdin>", line 1
print("hello", end="")
^
SyntaxError: invalid syntax
In Python3 it wouldn't happen:
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello", end="")
hello>>>

Related

None difference for Python versions

I've read a lot about Python versions differences but never met those
Python 2.7.18 (default, Mar 8 2021, 13:02:45)
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute '__eq__'
and
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
NotImplemented
I found it already being in Python 3.2
Python 3.2.6 (default, Jan 18 2016, 19:21:14)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None.__eq__("abc")
NotImplemented
What I wan't to know if it is told somewhere in docs or somewhere. Didn't find anything here. Any sources?

NixOS python package version pinning (E.g requests 1.0.0) in /etc/nixos/configuration.nix

I am currently playing around with nixos and python. I'm wondering how to pin python package versions in /etc/nixos/configuration.nix
What i tried is the following:
let my-python-packages = python-packages: with python-packages; [
pandas
"requests==1.0.0"
];
Expected result
[jane#nixos:~]$ python
Python 2.7.17 (default, Oct 19 2019, 18:58:51)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import requests
>>> requests.__version__
'1.0.0'
Actual result
Python 2.7.17 (default, Oct 19 2019, 18:58:51)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
After that i tried this in configuration.nix:
# and in the NixOS manual (accessible by running ‘nixos-help’).
{ config, pkgs, ... }:
let nixpkgs.config.packageOverrides = super: {
python = super.python.override {
packageOverrides = python-self: python-super: {
requests = python-super.requests.overrideAttrs
(oldAttrs: {
src = super.fetchPypi {
pname = "requests";
# sha256 hash of requests 1.0.0 as on pypi
sha256 = "f10d8fbcc02a58056ab44f79ff9b3f9fe78e410296527885250bbb36d15be8c6";
};
});
};
};
};
my-python-packages = python-packages: with python-packages;
[
pandas
requests
];
python-with-my-packages = pkgs.python2.withPackages my-
python-packages;
Actual result:
[jane#nixos:~]$ python
Python 2.7.17 (default, Oct 19 2019, 18:58:51)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> import requests
>>> requests.__version__
'2.22.0' # this should be 1.0.0
So how can i pin python packages to specific versions in /etc/nixos/configuration.nix ?

Python - print invalid syntax when using the 'end' argument [duplicate]

This question already has answers here:
Getting SyntaxError for print with keyword argument end=' '
(17 answers)
Closed 5 years ago.
In python 3, I'm tying to print a line without skipping a line as:
print('hello', end='')
And I'm getting this error message:
print('hello', end='')
^
SyntaxError: invalid syntax
What's going on? How do I correct this error?
Are you absolutely sure you're using Python 3, and not accidentally invoking Python 2?
~ $ python3
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
hello>>>
~ $ python2
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
File "<stdin>", line 1
print('hello', end='')
^
SyntaxError: invalid syntax
>>>

string.maketrans unicode handling: pypy vs CPython

Pypy doesn't seem to handle string.maketrans() when arguments are unicode, however CPython does:
$ python
Python 2.7.5 (default, Oct 11 2013, 14:51:32)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> string.maketrans(ur"-/[] ", ur"_____")
'\x00\x01\x02\x03\x04\x05\x06...'
$ pypy
Python 2.7.13 (c925e73810367cd960a32592dd7f728f436c125c, Jun 08 2017, 19:14:08)
[PyPy 5.8.0 with GCC 6.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> import string
>>>> string.maketrans(ur"-/[] ", ur"_____")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../pypy-5.8-linux_x86_64-portable/lib-python/2.7/string.py", line 78, in maketrans
buf[ord(fromstr[i])] = tostr[i]
TypeError: 'unicode' object cannot be interpreted as an index
Didn't find anything relevant on http://pypy.readthedocs.io/en/latest/cpython_differences.html.
Is this a bug of CPython or PyPy?
That's a "bug", i.e. an unexpected difference. Fixed in 7fe0041fccaa (see line 78 of https://bitbucket.org/pypy/pypy/raw/default/lib-python/2.7/string.py).

Return value of 'print' function?

I'm python beginner.
I am wondering what the return value of the 'print' function is.
I tried type(print(3)) and didn't work.
I tried to find the api document but I could only find the pprint function.
print() returns None in Python3.
$ python3
Python 3.3.2+ (default, Feb 28 2014, 00:52:16)
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
3
<class 'NoneType'>
In Python2 print is a statement, so doesn't return anything...
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print(3))
File "<stdin>", line 1
type(print(3))
^
SyntaxError: invalid syntax
...unless you use from __future__ import print_function
$ python2
Python 2.7.5+ (default, Feb 27 2014, 19:37:08)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import print_function
>>> type(print(3))
3
<type 'NoneType'>

Categories

Resources