I just found out this today:
If I have an existing file named a111, and I want to create a new file named A111 in the same directory with Python:
f = file('A111', 'w')
f.write('test')
f.close()
It will overwrite my file a111 and there's no A111!!
How do I prevent this from happening?
It is not due to python. It is due to the case-insensitivity of your underlying file system (I'm guessing HFS+ in your case?). From wikipedia:
Not all file systems in Unix-like systems are case-sensitive; by default, HFS+ in Mac OS X is case-insensitive
The solution is to use a case-sensitive file system, if you want one, or to use a different filename!
This does reproduce for me, actually.
nixon:~ matt$ touch a111
nixon:~ matt$ python
Python 2.7.2 (default, Nov 14 2011, 19:37:59)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f = file('A111', 'w')
>>> f.write('test')
>>> f.close()
>>>
nixon:~ matt$ cat a111
test
Also on a mac.
nixon:~ matt$ uname -a
Darwin nixon.local 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun 7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386
nixon:~ matt$ python --version
Python 2.7.2
I suspect you'll find that what's going on is that we're both using HFS, which is a case-insensitive filesystem.
A Mac's HFS+ filesystem is case-insensitive by default, unless you perform an installation from scratch - one of the installation options is turning on the case sensitivity.
Related
On my Linux machine, directly execute python command, it shows that my Python is UCS4 build.
Python 2.7.3 (default, Jan 8 2018, 17:43:28)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> if sys.maxunicode > 65535:
... print 'UCS4 build'
... else:
... print 'UCS2 build'
...
UCS4 build
However, when I call python in C++ program using
Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("if sys.maxunicode > 65535:\n print 'UCS4 build'\nelse:\n print 'UCS2 build'");
it prints "UCS2 build".
Other information from the python called by c++ are:
platform:Linux-2.6.32_1-19-0-0-x86_64-with-centos-6.3-Final
('Python', '2.7.5 (default, Apr 13 2016, 14:25:24)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)]')
('Python', '******/venv')
I have double checked the python executable path. They are from the same path, but python version and gcc version are different.
Anyone know the reason of this strange sympotom?
Well, I solved this problem. Using ldd command, I found that libpython2.7.so.1.0 pointed to a wrong default path, not the one printed before. Correct the LD_LIBRARY_PATH solved this confusion.....
There is a problem with readline in Python shell under Mavericks. Two issues, answered here in stackoverflow separately but not both at the same time.
a) segfault after entering the 2nd line and/or
b) line editing does not work.
I provide links to both issues below.
Basically one can either A) do nothing and enjoy segfaults while typing a second line in the shell, or B) - "fix" it by renaming readline.so to readline.so.disabled -- but in this case loosing editing capabilities in the Python shell.
I could not find a recipe that solves both issues. And all of the sudden it's really hard to use Python shell. Please help.
Here are more details:
a) The segfault issue is answered here and here:
Python crashing when running two commands (Segmentation Fault: 11)
Segmentation fault: 11 in OS X
b) the "up arrow" issue in python interpreter is answered here:
Seeing escape characters when pressing the arrow keys in python shell
Here is how to reproduce the problem on my system:
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1
>>> y=2
Segmentation fault
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%sudo mv ./lib/python2.7/lib-dynload/readline.so ./lib/python2.7/lib-dynload/readline.so.disabled
mymachine:/Library/Frameworks/Python.framework/Versions/2.7%python
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=1
>>> y=2
>>> ^[[A
File "<stdin>", line 1
^
SyntaxError: invalid syntax
This will not fix your system install, but I think a virtualenv would not have this problem. I use Python every day on Mavericks with a virtualenv and never have any trouble.
I'm trying to write a program in python that uses commands and files that are os dependent and I don't have access to a mac. What is the exact system name for the os.name() command?
Better yet what is the platform.system() answer. I have windows which of course is 'windows' and I have Linux which in my case is 'Linux' but what is mac? So right now I need the output for os.name() and platform.system(). Preferably a more recent version of OS X like either mountain lion or Maverick.
The platform name for all OS X releases is Darwin:
Python 2.7.6 (default, Apr 28 2014, 17:17:35)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.system()
'Darwin'
platform.system() uses os.uname(), and you can look up responses on the uname Wikipedia page.
I often have a collection of .py files, maybe split over a few folders, that all use some little special purpose library I've written that doesn't belong in my general PYTHONPATH. What's the cleanest way for me to get that library into the Python path for those scripts that need it? I'll be the only one ever to run any of these, and I will generally be doing so from Spyder.
I've tried with .pth files. This may not be the best way, but I can't get them to work regardless. For example, if I create a .pth file containing
/var
The running the following code out of the same directory via Spyder, I get output of True, False, True:
import sys
import site
print any(["python" in path for path in sys.path]) # True
print any(["/var" in path for path in sys.path]) # False
site.addsitedir(".")
print any(["/var" in path for path in sys.path]) # True
Here are the details of my environment:
Python 2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Nov 11 2013, 10:49:09)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Imported NumPy 1.7.1, SciPy 0.13.0, Matplotlib 1.3.1
When I start Python from Mac OS' Terminal.app, python recognises the encoding as UTF-8:
$ python3.0
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'UTF-8'
This works the same for python2.5.
But inside Emacs, the encoding is US-ASCII.
Python 3.0.1 (r301:69556, May 18 2009, 16:44:01)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.stdout.encoding
'US-ASCII'
How do I make Emacs communicate with Python so that sys.stdout knows to use UTF-8?
Edit: Since I don't have the rep to edit the accepted answer, here is precisely what worked for me on Aquaemacs 1.6, Mac OS 10.5.6.
In the python-mode-hook, I added the line
(setenv "LANG" "en_GB.UTF-8")
Apparently, Mac OS requires "UTF-8", while dfa says that Ubuntu requires "UTF8".
Additionally, I had to set the input/output encoding by doing C-x RET p and then typing "utf-8" twice. I should probably find out how to set this permanently.
Thanks to dfa and Jouni for collectively helping me find the answer.
Here is my final python-mode-hook:
(add-hook 'python-mode-hook
(lambda ()
(set (make-variable-buffer-local 'beginning-of-defun-function)
'py-beginning-of-def-or-class)
(define-key py-mode-map "\C-c\C-z" 'py-shell)
(setq outline-regexp "def\\|class ")
(setenv "LANG" "en_GB.UTF-8"))) ; <-- *this* line is new
check your environment variables:
$ LANG="en_US.UTF8" python -c "import sys; print sys.stdout.encoding"
UTF-8
$ LANG="en_US" python -c "import sys; print sys.stdout.encoding"
ANSI_X3.4-1968
in your python hook, try:
(setenv "LANG" "en_US.UTF8")