Virtualenv “. venv/bin/activate” vs “source venv/b

2020-06-01 08:36发布

lets say i created a virtualenv called venv (virtualenv venv)

From reading tutorials, i read there are 2 ways to activate virtual env:

  1. . venv/bin/activate

  2. source venv/bin/activate

I think they both accomplish the same thing, but i dont understand whats going on.

Also for number 1, doesnt the "." just mean the current folder? but it doesnt work if i just type in "venv/bin/activate" without the "."

any help would be great!

2条回答
Fickle 薄情
2楼-- · 2020-06-01 08:56

. and source do exactly the same thing, with the only difference being that while source is more readable, it may not be available in all shells.

The command runs the contents of the script within the current shell, and this is important in the case of activate, because one of the things that the script does is exports and modifies environment variables within your current shell.

If you were to run it using ./path/to/activate, the script will be run within a subshell and all environment variables that are set will be lost once the script completes and the subshell terminates.

Also for number 1, doesn't the "." just mean the current folder?

. has a different meaning depending on the context. It only means "current folder" when used as (or part of) a path.

From http://en.wikipedia.org/wiki/Dot_%28Unix%29:

The dot command is not to be confused with a dot file, which is a dot-prefixed hidden file or hidden directory.


As an aside, I would suggest that you take a look at virtualenvwrapper which provides additional wrapper commands that make virtualenv much easier to use.

Using virtualenvwrapper, switching to an evironment is done simply by calling:

workon YOUR_ENV
查看更多
小情绪 Triste *
3楼-- · 2020-06-01 08:59

The . command is essentially an alias for the source. They both execute a given script in the current shell without forking a new shell.

Here are some nice examples.

查看更多
登录 后发表回答