Tag Info

Hot answers tagged

23

Python imports a large number of files at startup: % python -c 'import sys; print len(sys.modules)' 39 Each of these requires an even greater number of attempts at opening a Python file, because there are many ways to define a module: % python -vv -c 'pass' # installing zipimport hook import zipimport # builtin # installed zipimport hook # trying ...


22

The shebang expects a full path to the interpreter to use so the following syntax would be incorrect: #!python Setting a full path like this would work: #!/usr/local/bin/python but would be non portable as python might be installed in /bin or /opt/python/bin or wherever location. Using env #!/usr/bin/env python is a method allowing a portable way ...


20

The shebang line (from “sharp bang”, i.e. #!) is processed by the kernel. The kernel doesn't want to know about environment variables such as PATH. So the name on the shebang line must be an absolute path to an executable. You can also specify an additional argument to pass to that executable before the script name (with system-dependent restrictions I won't ...


14

The first place to check is if there's a backport, but there isn't, which isn't surprising since maverick has vim 7.2 too. The next thing to try is if someone's put up a repository with vim 7.3 packages somewhere, preferably a PPA. There are many PPAs with vim, including several with 7.3 (not an exhaustive list). If you don't find a binary package anywhere ...


14

sudo python -m SimpleHTTPServer 80 Ports below 1024 require root privileges. As George added in a comment, running this command as root is not a good idea - it opens up all kinds of security vulnerabilities. However, it answers the question.


13

The shebang line is very limited. Under many unix variants (including Linux), you can have only two words: a command and a single argument. There is also often a length limitation. The general solution is to write a small shell wrapper. Name the Python script foo.py, and put the shell script next to foo.py and call it foo. This approach doesn't require any ...


12

The recommended way of having multiple Python versions installed is to install each from source - they will happily coexist together. You can then use virtualenv with the appropriate interpreter to install the required dependencies (using pip or easy_install). The trick to easier installation of multiple interpreters from source is to use: sudo make ...


11

I'm not expert, but I believe that you shouldn't specify exact python version to use and leave that choice to system/user. Also you should use that instead of hardcoding path to python in script: #!/usr/bin/env python or #!/usr/bin/env python3 (or python2) It is recommended by Python doc in all versions: A good choice is usually #!/usr/bin/env ...


10

This sounds like a perfect application for virtualenv, a very popular tool for creating isolated Python environments. This is a sample command to specify the version of Python $ virtualenv --python=/usr/bin/python2.6 myvirtualenv


10

I found a nice answer on SO explaining the different fields: Real is wall clock time - time from start to finish of the call. This is all elapsed time including time slices used by other processes and time the process spends blocked (for example if it is waiting for I/O to complete). User is the amount of CPU time spent in user-mode code (outside ...


10

I will explain it from another perspective. To be fair, bc has advantage since it doesn't have to read anything from the disk and only needs its blob/binaries while python has to import a series of modules + reading a file. So your test might be biased towards bc. To actually test it you should use bc -q file where file contains: 6^6^6 quit Changing ...


9

When you fire off something with sudo a couple of environment variables get set, specifically I think you are looking for SUDO_UID. These should be accessible to any program running through the usual channels of accessing environment variables. You can see the other things set by cheating like this from a shell: sudo env | grep SUDO


9

You generally can't see the output of anther person's program. See over in that column where your grep command is running on tty pts/3, and your friend's is ?, which means it's detached from the terminal. You could see where the output is going with ls -l /proc/7494/fd/ (where 7494 is the process ID of your friend's process) — although if you're not ...


8

Ok, that makes things a bit clearer. command-not-found is a python program, which runs when your command is not something found on the system. (Its function is to suggest alternatives and corrections in case of mistyping etc.) See /usr/bin/command-not-found. It is trying to import the CommandNotFound module and is unable to, clearly pointing to a screwed up ...


8

Changing the default Python (or Perl, etc) on an OS is really bad idea. This interpreter is actually part of the OS and there may well be other OS components that are written specifically to work with that version of the interpreter. For example on Redhat the yum tool that performs system software updates is a python application. You really don't want to ...


8

Shebang lines do not undergo variable expansion, so you cannot use $FOO/MyCustomPython as it would search for an executable named dollar-F-O-O-... An alternative is to have your shebang point to a shell script as the interpreter, and this shell script can then use environment variables to locate the correct one and exec it. Example: create a mypython.sh ...


8

As others have noted, the shebang line must refer to an actual file that exists. Since different systems can have binaries installed to different locations, this is a weakness of script portability. One way to solve this is with links, as others have mentioned. Another way is to edit the script to refer to the right path on your system. One more way to ...


8

Getting a variable to Python Since variable substitution occurs before text is passed from the heredoc to python's standard input, you can throw the variable right in the script. python - <<EOF some_text = "$some_text" EOF If some_text was "test", python would see some_text = "test". If you want to be able to pull your Python code right into a ...


7

How do I fix this mess? Nothing more than reinstalling python. It will undo your change (the symlink). Why do you want it as default? Each time you need it, just use python2.7 or include #!/usr/bin/python2.7 (the shebang) at the beginning of your (executable) scripts. If you insist on having python2.7 as system-wide default, use a later release of ...


7

Unix/Linux file systems do not rely on extensions the way windows does. You should not need the .py at the end of a file to run it. You can run the file by either calling it with the interpreter: python ScriptFile Or by marking it executable and defining the interpreter on the first line (e.g. #!/usr/bin/python). If you are unable to execute the file ...


7

A regex in Python, either the search or match methods, returns a Match object or None. For grep -v equivalent, you might use: import re for line in sys.stdin: if re.search(r'[a-z]', line) is None: sys.stdout.write(line) Or more concisely: import re; sys.stdout.writelines([line for line in sys.stdin if re.search(r'[a-z]', line) is None])


7

Based on some ideas from a few comments, I managed to cobble together a truly ugly hack that seems to work. The script becomes a bash script wraps a Python script and passes it to a Python interpreter via a "here document". At the beginning: #!/bin/bash ''':' vers=( /usr/bin/python2.[5-7] ) latest="${vers[$((${#vers[@]} - 1))]}" if !(ls $latest ...


7

On Ubuntu can use upstart to do so, better than a cron job. You put a config setup in /etc/init and make sure you specify respawn It could be a minimal file /etc/init/testing.conf (edit as root): chdir /your/base/directory exec python testing.py respawn And you can test with /your/base/directory/testing.py: from __future__ import print_function import ...


6

This is bizarre, but unless it is something that Stack Exchange is doing, I think the problem you are having is that you are using "fancy quotes" rather than "". Whatever editor you are using may have replaced the standard quote character(Unicode U+0022) with stylized open and close quotes (U+201C and U+201D). sh doesn't like this very much. When I ...


6

Remove your system wide installation of pip: sudo apt-get purge python-pip Then install a fresh copy of pip: curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | sudo python Tested on ubuntu 10.04 i686 I suggest you to use virtualenv. For further details see the Official pip documentation


6

The problem is your designated output from dd goes to STDERR and not STDOUT so you have to redirect STDERR as well and not only STDOUT. For bash and zsh you can use |& instead of | which will also redirect STDERR to STDIN of the second command, e.g: dd if=/dev/urandom of=/dev/null bs=1K count=10000 |& awk '/copied/ {print $8 " " $9}' The more ...


6

This is probably an issue with your PYTHONPATH environment variable, or a lack of one. Using apt-get, matplotlib will be installed into the /usr directory prefix. So you'll find the whole package installed somewhere like /usr/lib/python2.7/site-packages/matplotlib. You can confirm this with a command like: dpkg -L python-matplotlib. When you separately ...


6

As your requirements state a known list of binaries, you could do it in Python with the following. It wouldn't work past a single digit minor/major version of Python but I don't see that happening any time soon. Runs the highest version located on disk from the ordered, increasing list of versioned pythons, if the version tagged on the binary is higher ...


6

The shebang line can only specify a fixed path to an interpreter. There's the #!/usr/bin/env trick to look up the interpreter in the PATH but that's it. If you want more sophistication, you'll need to write some wrapper shell code. The most obvious solution is to write a wrapper script. Call the python script foo.real and make a wrapper script foo: ...


6

The #! (shebang) line must specify an executable that actually exists. If both work on your system, it means you have a python installed in both places. Or, possibly, a symlink. Normally, you'd get an error attempted to use an interpreter that doesn't exist: anthony@Zia:~$ /tmp/test bash: /tmp/test: /usr/local/bin/python: bad interpreter: No such file or ...



Only top voted, non community-wiki answers of a minimum length are eligible