Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use python's -S option?

Tags:

python

The -S option to python is defined by the documentation as "Disable the import of the module site and the site-dependent manipulations of sys.path that it entails." I've found that python startup on my machine is more than twice as fast, sometimes much more, when I use this option. For example, on one (slow) machine:

$ time python -c 'print "hello"'   
hello
python -c 'print "hello"'  0.14s user 0.03s system 85% cpu 0.204 total

$ time python -Sc 'print "hello"'
hello
python -Sc 'print "hello"'  0.02s user 0.01s system 73% cpu 0.038 total

That's a 5.3x speedup. And it seems to work fine, at least with the scripts I've tried. What are the disadvantages to using it?

like image 775
apenwarr Avatar asked Jul 03 '12 19:07

apenwarr


People also ask

Is using Python safe?

Yes, Python is safe for your computer. If you use a Mac, Python is already installed on your computer, in fact. Installing Python won't expose you to any security issues - it won't allow attackers to do anything extra you couldn't already do from the shell/terminal.

Is Python safe from hackers?

Python is Easy to Access and Use Premium privacy software goes further than looking for existing hacking methods; it also highlights any suspicious behavior that could be the result of a new cybercrime tactic. Use a high-quality antivirus to keep your system safe from all varieties of attack.

Is Python a security risk?

But like all programming languages, Python is not immune to security threats. Secure coding best practices must be adopted to avoid risks from attackers. In this post, we'll explore Python security best practices that should employed when building secure application.

What is Python's main purpose?

Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general-purpose language, meaning it can be used to create a variety of different programs and isn't specialized for any specific problems.


2 Answers

It's probably not a good idea. Among other things, it means that the site-packages directory won't be added to the path, so you won't be able to import anything but the standard lib modules:

python -Sc "import numpy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named numpy

You can look at site.py yourself to see what it's doing. It's just a module in the regular library directory. At least on my system, it looks like it does four main things:

  • sets up the site-packages paths
  • sets the default encoding
  • defines a few helper functions for interactive use (quit and help)
  • sets up user-specific site customization

The first one is probably the most critical, as mentioned above. The second could be important for doing string I/O depending on your system's locale settings (i.e., you may get errors if the default encoding isn't correctly set). The third is probably not that important. The last one could be important if you like to have per-user path customizations (letting users have their own personal library directories, etc.).

like image 73
BrenBarn Avatar answered Sep 18 '22 11:09

BrenBarn


The -S flag does the following:

don't imply 'import site' on initialization

This means the module site is not imported during initialization of Python. A brief description is that this module "will append site-specific paths to the module search path and add a few builtins". Not doing all this work will indeed make the startup faster.

Using the documentation as a guide, the -S flag results in:

  • No additional modules added to sys.path. You can compare the difference by starting python and python -S and doing the following in both import sys; print sys.path. Many modules won't be available so you won't be able to import them.
  • Custom site initialization code won't be run (this can be defined in a module called sitecustomize).
  • Custom initialization code won't be run (this can be defined in a module called usercustomize).

The short answer to your question is: yes, it makes Python startup faster but many modules and customization code won't be available or possible.

If you primarily import your own modules and write your own computations / code then the -S flag is fine. But if you have a Python installation with modules installed in different places then you won't be able to use them with the -S flag.

like image 31
Simeon Visser Avatar answered Sep 20 '22 11:09

Simeon Visser