Can I run the python interpreter without generating the compiled .pyc files?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
In 2.5, theres no way to suppress it, other than measures like not giving users write access to the directory.
In python 2.6 and 3.0 however, there may be a setting in the sys module called "dont_write_bytecode" that can be set to suppress this. This can also be set by passing the "-B" option, or setting the environment variable "PYTHONDONTWRITEBYTECODE"
I have several test cases in a test suite and before I was running the test suite in the Mac Terminal like this:
Running the command this way my directory was being populated with .pyc files. I tried the below stated method and it solved the issue:
This method works if you are importing test cases into the test suite and running the suite on the command line.
As far as I know python will compile all modules you "import". However python will NOT compile a python script run using: "python script.py" (it will however compile any modules that the script imports).
The real questions is why you don't want python to compile the modules? You could probably automate a way of cleaning these up if they are getting in the way.
You can set
sys.dont_write_bytecode = True
in your source, but that would have to be in the first python file loaded. If you executepython somefile.py
then you will not getsomefile.pyc
.When you install a utility using
setup.py
andentry_points=
you will have setsys.dont_write_bytecode
in the startup script. So you cannot rely on the "default" startup script generated by setuptools.If you start Python with python file as argument yourself you can specify
-B
:somefile.pyc
would not be generated anyway, but no.pyc
files for other files imported too.If you have some utility
myutil
and you cannot change that, it will not pass -B to the python interpreter. Just start it by setting the environment variablePYTHONDONTWRITEBYTECODE
:Solution for
ipython 6.2.1 using python 3.5.2
(Tested on Ubuntu 16.04 and Windows 10):Ipython
doesn’t respect%env PYTHONDONTWRITEBYTECODE =1
if set in theipython
interpretor or during startup in~/.ipython/profile-default/startup/00-startup.ipy
. Instead using the following in your~.ipython/profile-default/startup/00-startup.py