Note that there are some explanatory texts on larger screens.

plurals
  1. POpython not reading the shell environment variable correctly (missing leading "equal" sign)
    primarykey
    data
    text
    <p>My shell's (specifically <code>zsh</code>) output for a variable is different from Python's <code>os.environ</code> output.</p> <p>I encountered this when I was trying to make a test Django site following what I'm reading in Two Scoops of Django.</p> <p>I'm trying to put the <code>SECRET_KEY</code> value into my <code>virtualenv</code>'s <code>bin/activate</code> script.</p> <p>Here's what I placed at the end of <code>bin/activate</code>:</p> <pre><code>export SECRET_KEY="=43v#b7jdiam3#j1$02s^#ul$uc$=32g=(+ofl=a&amp;$r_^m8-4z" </code></pre> <p>Doing an <code>echo $SECRET_KEY</code> after activating the virtualenv gives:</p> <pre><code>=43v#b7jdiam3#j1s^#ulg=(+ofl=a&amp;^m8-4z </code></pre> <p>as expected. However, when I access that variable in Python using this:</p> <pre><code>import os print os.environ["SECRET_KEY"] </code></pre> <p>it gives me...</p> <pre><code>43v#b7jdiam3#j1s^#ulg=(+ofl=a&amp;^m8-4z </code></pre> <p>(without the leading <em>equals</em> sign).</p> <p>I don't know where the source of the discrepancy is. I don't even know if this is a problem at all but it seems odd that the shell and python reads them differently.</p> <p>The Django project seems to run alright. Are there going to be problems with this difference in the environment variable?</p> <p><strong>Update:</strong></p> <p>I have replaced the export line with single quotes. Here are the results:</p> <pre><code>[1] =43v#b7jdiam3#j1$02s^#ul$uc$=32g=(+ofl=a&amp;$r_^m8-4z [2] 43v#b7jdiam3#j1$02s^#ul$uc$=32g=(+ofl=a&amp;$r_^m8-4z </code></pre> <p>where [1] is the result of <code>echo $SECRET_KEY</code> on the terminal while [2] is the result of <code>import os; print os.environ["SECRET_KEY"]</code> on the python shell.</p> <p><strong>Update 2:</strong></p> <p>I'm using Homebrew Python 2.7.6 on Mac OS X 10.8 and Homebrew ZSH 5.0.4.</p> <p>I continued my investigation and used bash instead of zsh. It worked properly!</p> <p>Now, is this a problem on <code>zsh</code> or in <code>virtualenv</code>?</p> <p><strong>Update 3:</strong></p> <p>I did the <code>export</code> line outside of virtualenv using <code>zsh</code> and checked the variable again in python. The problem still exists. Therefore, the problem, I think, is with <code>zsh</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload