1

From the website ( https://channel9.msdn.com/Blogs/jsturtevant/Setting-up-Project-Oxford-for-Python-on-Windows ) at about 4:27, the author opens the activate.bat file by using code .env\Scripts\activate.bat, however, when I run that, they'll show bash: code: command not found. I have also tried with vscode .env\Scripts\activate.bat to no avail.

I remember that from: python.exe file not found; creating "virtual environment" you suggested using source .env/bin/activate ; however, I soon realize that it only creates venv and does not open the activate.bat file on VSCode for me to edit the API code in, When I force open it using the Code-OSS, it shows an empty file. However, when I try to open it using notepad, it shows the code in it as follows:

# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

deactivate () {
    # reset old environment variables
    if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
        PATH="$_OLD_VIRTUAL_PATH"
        export PATH
        unset _OLD_VIRTUAL_PATH
    fi
    if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then
        PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
        export PYTHONHOME
        unset _OLD_VIRTUAL_PYTHONHOME
    fi

    # This should detect bash and zsh, which have a hash command that must
    # be called to get it to forget past commands.  Without forgetting
    # past commands the $PATH changes we made may not be respected
    if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
        hash -r
    fi

    if [ -n "$_OLD_VIRTUAL_PS1" ] ; then
        PS1="$_OLD_VIRTUAL_PS1"
        export PS1
        unset _OLD_VIRTUAL_PS1
    fi

    unset VIRTUAL_ENV
    if [ ! "$1" = "nondestructive" ] ; then
    # Self destruct!
        unset -f deactivate
    fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="/home/pi/happy-image-tester-django/env"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use if (set -u; : $PYTHONHOME) ; in bash
if [ -n "$PYTHONHOME" ] ; then
    _OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
    unset PYTHONHOME
fi

if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
    _OLD_VIRTUAL_PS1="$PS1"
    if [ "x(env) " != x ] ; then
 PS1="(env) $PS1"
    else
    if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
        # special case for Aspen magic directories
        # see http://www.zetadev.com/software/aspen/
        PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
    else
        PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
    fi
    fi
    export PS1
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands.  Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
    hash -r
fi

Therefore, may I ask if I can just key in the 3 lines of codes:

line 61: SET “OXFORD-KEY=________”
62
63 :END

into the notepad directly and save it? I believe I cant do that as the file seem to have compressed into the format suitable for raspbian somewhere when the program is installed. Hence, may I know what code I should key in instead to open the bat file properly to be able to edit it? Thank you!!!

<----------------------------------------------------------------------------->

3rd python script (combine.py) from https://stackoverflow.com/questions/31601996/combining-two-python-scripts-to-run-as-one-program:

def code1():
    import capture.py

def code2():
    import manage.py

code1()
code2()

Upon runninng:

(env) pi@raspberrypi:~/happy-image-tester-django $  OXFORD_KEY=1234567890 python capture.py runserver

Should I do the 1st method above or the 2nd method which is inserting the oxford_key into the combine.py scripts as shown below:

def code1():
    import capture.py

def code2():
    import manage.py

code1()
OXFORD_KEY=1234567890
code2()

and then runs this directly without having to input the oxford key in the command prompt any longer: (env) pi@raspberrypi:~/happy-image-tester-django $ python capture.py runserver

0

That line is setting an environment variable called OXFORD_KEY. That's not an unusual thing to do for sensitive data (e.g. passwords and tokens) as opposed to hard-coding it into the program.

While you could edit the activate script to create the environment variable there, I personally wouldn't. They aren't really designed to be tampered with, and as you can see, it's not a trivially short script and so I'd err on the side of doing things more conventionally.

Consider some of the more standards ways of setting an environment variable:

1. For one command

OXFORD_KEY=yourkeyhere <command>

If you know specifically which command needs the environment setting, you can set the environment variable just for one time by setting it, then typing the command you want to run.

I suspect if you did this, you'd need to run OXFORD_KEY=key python manage.py runserver.

2. For a terminal session

This is probably the most straightforward option — run:

export OXFORD_KEY=key

in your terminal, on a new line. Then, the variable is set until you close your terminal.

3. Using .bashrc

If you wanted to set the variable permanently, add

export OXFORD_KEY=key

to .bashrc in your home directory. Note that if the key is sensitive, this isn't such a good idea.


Ultimately, if you want a quick fix, run:

export OXFORD_KEY=key

And then skip the step of editing the activate script. You'll need to re-enter the key at every terminal restart — if that's a problem, see "Using .bashrc" for a more permanent solution.


For running the scripts in series, you probably don't want to use a Python script, but rather a Bash shell script. Create a file called run.sh with the following contents:

export OXFORD_KEY=yourkey
python capture.py 
python manage.py

(pass any command line options as appropriate)

Then, run the following, to make your script executable:

chmod +x ./run.sh

And to run your script:

./run.sh

Bash scripts, in their simplest form, just act as if each command was typed out on a new line in your terminal. This should be the most straightforward way for you to run multiple commands in series like this.

The export will set the environment variables for the length of the script, but won't last after the script ends.

  • Thank you! I'll use the first suggestion for now as my key subscription expires periodically. Also, may I ask 1 seemingly simple qn? I have another file called capture.py, and I want it to run before manage.py (this file); hence, may I know if I can create another script to merge the 2 scripts using the method shown here: stackoverflow.com/questions/31601996/… ; by "10-yr-old"'s comment. I'm not very sure as to why that comment have -1 as not useful comment, does it not work that way? The merge codes are edited in the post above. – GoodCodes Jan 21 '18 at 3:34
  • @GoodCodes I've edited with my suggestions; while you could do it from Python, the more obvious way is to use a shell script to run your commands. That answer is downvoted not because it's actually wrong... more that it's bad practice when programming, and the other solutions are cleaner. – Aurora0001 Jan 21 '18 at 12:15
  • Ohh,thankssss! I see, I see; may I ask that since you said that using .bashrc would set the variable permanently, am I still able to change the variable (OXFORD_KEY) using the run.sh method as my oxford key would be changed every couple of weeks? And hence, does it mean that I have to edit the file "run.sh" -> chmod +x ./run.sh -> ./run.sh everytime I change the oxford key? – GoodCodes Jan 21 '18 at 13:55
  • 1
    The chmod only needs to be done once — it just sets the file to be executable, and that's saved forever. If you put OXFORD_KEY in the run.sh script, you needn't bother with .bashrc; just edit run.sh to update OXFORD_KEY, and then you can run it as many times as you like until the key expires. – Aurora0001 Jan 21 '18 at 13:56
  • 1
    You built VS Code from source, so it isn't added to your PATH and hence isn't available as a command by default. I looked at where you saved it in your previous question, and the launch script for you is at /home/youruser/vscode/scripts/code.sh. You will therefore need to run /home/youruser/vscode/scripts/code.sh <file> in your terminal to launch it. – Aurora0001 Jan 21 '18 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.