Python framework-Virtual Environment configuration, python framework-virtual

Source: Internet
Author: User

Python framework-Virtual Environment configuration, python framework-virtual

During the development process, different projects are often to be developed on the same computer. Different projects may require packages of different versions. To solve this problem, a virtual environment is introduced.

Configure the virtual environment:

1. Install the virtual environment:

sudo pip3 install virtualenv  

 

2. Install the virtual environment extension package:

sudo pip3 install virtualenvwrapper

 

3. modify the configuration file. bashrc in the user's home directory and add the following content:

export WORKON_HOME=$HOME/.virtualenvssource /usr/local/bin/virtualenvwrapper.sh

 

 

4. Use the source. bashrc command to make the configuration file take effect.

The following error may occur:

/usr/bin/python: No module named virtualenvwrapper  virtualenvwrapper.sh: There was a problem running the initialization hooks.   If Python could not import the module virtualenvwrapper.hook_loader,  check that virtualenvwrapper has been installed for  VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is  set properly. 

 

The system uses the interpreter of python2 by default. Therefore, we need to add the interpreter of python3 to the environment variable:

4.1 open the system environment variable file:

vi /etc/profile

4.2 Add python3:

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

 

4.3 Use the source/etc/profile command to make the configuration file take effect.

4.4 run the source. bashrc command again to make the configuration file take effect.

5. Run the following command to create the python3 virtual environment:

Mkvirtualenv-p python3 Virtual Environment name
  • After the creation is successful, it will automatically work in this virtual environment.
  • Networking is required to create a virtual environment.
  • Working in the virtual environment, "(Virtual Environment name)" will appear at the beginning of the prompt )".
  • All virtual environments are located in the hidden directory. virtualenvs under/home/python.

6. You can completely export the packages required for the complete project you have used for installation:

6.1 first import the installation package in the previous project to a folder:

pip freeze > package.txt

 

6.2switch to the newly created virtual environment, and install the package.txt package:

Pip install-r./package.txt # Put package.txt under the Home directory, and the system also switches to the home directory.

 

This may happen:

The installation is stopped when the red font appears. Solution: Observe which package is described in the red font, change the version number or comment it out, and install the package separately after installation.

When the last few lines are displayed, the installation is successful:

6.3 cd. virtualenvs/django_py3/lib/python3.5/site-packages:

7. Run the following command to exit the virtual environment:

deactivate

 

8. The command to delete a virtual environment is as follows:

Rmvirtualenv Virtual Environment name

 

9. Run the "workon tab" command to view the virtual environment.

10. Enter the virtual environment command: workon Virtual Environment name

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.