1

all

I have a question about an format that to run shell script I encounter this problem is that I setup a virtual environment

But when I type

. venv/bin/activate

The code has no problem and run it successfully.But I know the other way to run it using

 ./venv/bin/activate

the bash prompt an error saying I don't have permission to do that.

I don't know what's the difference between them, any help would be appreciate

Wallace
  • 151
  • 1
  • 9

1 Answers1

2

The activate script created by virtualenv isn't actually a standard script for executing anything, but it's something that defines further environment variables to an existing bash session. Note the comment in that file:

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

The . venv/bin/activate is actually a synonym to sourcevenv/bin/activate. In essence, what virtualenv does is to execute a number of statements in the current shell (sourcing the contents of the file into here), rather than starting a new shell process as if you would execute a binary (i.e. ./venv/bin/python).

metatoaster
  • 17,419
  • 5
  • 55
  • 66
  • another way to think of sourcing files is like `#include /path/to/envVarFile`. (Depending on your language). Good luck to all. – shellter Oct 21 '15 at 00:23
  • Hi, thanks for your quick apply, another question is, is " . runfile" this format that I can use everywhere?, because when I use" vim newfile" , and write some shell script in it, I can't use". newfile" to run the script in shell, it says no such file or directory, like your answer, I should use " source newfile" first and then use". newfile" to run it? – Wallace Oct 21 '15 at 00:28
  • You need to write out the file to disk before you can actually use the file. `vim` by default does not create `newfile` unless `:w` (or equivalent) is called in cmdline mode. Also there are no difference between `. $file` vs `source $file`, they both execute the file in-place of the current shell. – metatoaster Oct 21 '15 at 00:31
  • right, thanks a lot, now I get from @Hackworth that The . command is essentially an alias for the source. but I indeed :wq it before I try to run it, so I don't know how can I use ". newfile", because I have ever used it in this way, but script is written by other people, I try in my way, it doesn't work. – Wallace Oct 21 '15 at 00:34
  • 1
    Try a more simple example, maybe like so: `echo $FOO` and see that an empty line, then create a file named `foo` (in vim or whatever) with following: `export FOO=1`, then `. foo`, then `echo $FOO`. – metatoaster Oct 21 '15 at 00:39