设置python 虚拟环境 virtualenv django 虚拟环境

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/development_environment

Ubuntu virtual environment setup
After installing Python and pip you can install virtualenvwrapper (which includes virtualenv). The official installation guide can be found here, or follow the instructions below.

Install the tool using pip3:

sudo pip3 install virtualenvwrapper
Then add the following lines to the end of your shell startup file (this is a hidden file name .bashrc in your home directory). These set the location where the virtual environments should live, the location of your development project directories, and the location of the script installed with this package:

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=' -p /usr/bin/python3 '
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
Note: The VIRTUALENVWRAPPER_PYTHON and VIRTUALENVWRAPPER_VIRTUALENV_ARGS variables point to the normal installation location for Python3, and source /usr/local/bin/virtualenvwrapper.sh points to the normal location of the virtualenvwrapper.sh script. If the virtualenv doesn't work when you test it, one thing to check is that Python and the script are in the expected location (and then change the startup file appropriately).

You can find the correct locations for your system using the commands which virtualenvwrapper.sh and which python3.

Then reload the startup file by running the following command in the terminal:

source ~/.bashrc
At this point you should see a bunch of scripts being run as shown below:

virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/postmkproject
...
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/get_env_details
Now you can create a new virtual environment with the mkvirtualenv command.

macOS virtual environment setup
Setting up virtualenvwrapper on macOS is almost exactly the same as on Ubuntu (again, you can follow the instructions from either the official installation guide or below.

Install virtualenvwrapper (and bundling virtualenv) using pip as shown.

sudo pip3 install virtualenvwrapper
Then add the following lines to the end of your shell startup file.

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
Note: The VIRTUALENVWRAPPER_PYTHON variable points to the normal installation location for Python3, and source /usr/local/bin/virtualenvwrapper.sh points to the normal location of the virtualenvwrapper.sh script. If the virtualenv doesn't work when you test it, one thing to check is that Python and the script are in the expected location (and then change the startup file appropriately).

For example, one installation test on macOS ended up with the following lines being necessary in the startup file:

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
export PROJECT_HOME=$HOME/Devel
source /Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh
You can find the correct locations for your system using the commands which virtualenvwrapper.sh and which python3.

These are the same lines as for Ubuntu, but the startup file is the differently named hidden file .bash_profile in your home directory.

Note: If you can't find .bash_profile to edit in the finder, you can also open this in the terminal using nano.

The commands look something like this:

cd ~ # Navigate to my home directory
ls -la #List the content of the directory. YOu should see .bash_profile
nano .bash_profile # Open the file in the nano text editor, within the terminal
# Scroll to the end of the file, and copy in the lines above
# Use Ctrl+X to exit nano, Choose Y to save the file.

Then reload the startup file by making the following call in the terminal:

source ~/.bash_profile
At this point, you may see a bunch of scripts being run (the same scripts as for the Ubuntu installation). You should now be able to create a new virtual environment with the mkvirtualenv command.

Windows 10 virtual environment setup
Installing virtualenvwrapper-win is even simpler than setting up virtualenvwrapper because you don't need to configure where the tool stores virtual environment information (there is a default value). All you need to do is run the following command in the command prompt:

pip3 install virtualenvwrapper-win
Now you can create a new virtual environment with the mkvirtualenv command

Creating a virtual environmentSection
Once you've installed virtualenvwrapper or virtualenvwrapper-win then working with virtual environments is very similar on all platforms.

Now you can create a new virtual environment with the mkvirtualenv command. As this command runs you'll see the environment being set up (what you see is slightly platform specific). When the command completes the new virtual environment will be active — you can see this because the start of the prompt will be the name of the environment in brackets (below we show this for Ubuntu, but the final line is similar for Windows/macOS).

$ mkvirtualenv my_django_environment

Running virtualenv with interpreter /usr/bin/python3
...
virtualenvwrapper.user_scripts creating /home/ubuntu/.virtualenvs/t_env7/bin/get_env_details
(my_django_environment) ubuntu@ubuntu:~$
Now you're inside the virtual environment you can install Django and start developing.

Note: From now on in this article (and indeed the module) please assume that any commands are run within a Python virtual environment like the one we set up above.

Using a virtual environmentSection
There are just a few other useful commands that you should know (there are more in the tool documentation, but these are the ones you'll use regularly):

deactivate — Exit out of the current Python virtual environment
workon — List available virtual environments
workon name_of_environment — Activate the specified Python virtual environment
rmvirtualenv name_of_environment — Remove the specified environment.

原文地址:https://www.cnblogs.com/HHHAI/p/11128784.html