简体   繁体   中英

Avoid .pyc Files in Python 3.x?

Currently, I'm working with Python 3.4. How could I avoid .pyc or .pyo files ( __pycache__ ) without editing the commandline?

The following command works perfectly Using terminal: python3 -B something.py

But these are not working in Python 3 (or are they??):

import os

os.environ['PYTHONDONTWRITEBYTECODE'] = "whatever"

according to the instructions , if it is set to a non-empty string, it should work. But I couldn't.

What am I missing?

I believe that environment variable must be set before the Python interpreter starts. Setting it from within a Python script seems to be too late (the interpreter has already loaded the script and generated the bytecode file).

The environment variable is read by the executable when it starts up. Setting it in the script can have no effect for the current interpreter. You must do it before the executable is run.

  • Python 3 will create __pycache__ directories with version-related compiled python inside. .pyc is from Python 2.

  • You do need to specify this on the command line OR in the environment while starting python, not after. But if you are writing an executable script in an environment that supports shebang lines, you can do something like:

#!/usr/bin/env python3 -B
  • Since your question specifies "without using the command line", you could set the PYTHONDONTWRITEBYTECODE=1 environment variable for your system/shell.

check this .. set env variable before executing export PYTHONDONTWRITEBYTECODE="" set this in .bashrc or alias python3="python3 -B"

.. How to avoid .pyc files?

but you will get pycache folder for python3 in the imported module level

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM