Ubuntu系統下無法使用pip將安裝包安裝到Python虛擬環境中
- Background
All I want to do is to Install and deploy Django into Python virtual environment to develope a web application, which is proceed following a course in book 《python程式設計:從入門到實踐》
I have python2.7 and python3.5 both in my computer.
The problem is that after I created and activated the virtual environment and prepared to install the Django module in the virtual environment with pip, I can only install it in the whole environment not the virtual one.
- Steps
1. Create a virtual environment
Enter the directory where you want to install the virtual environment, then use $ python3 -m venv ll_env to create a virtual environment named by 'll_env'. Please mind that the venv module is only avaliable in version >= python3.3.
After doing that, I got a directory named by 'll_env' which has such structure:
2. Activate the virtual environment
Create a virtual environment doeson't mean that you can directly use it. Before that, you should activate it first. The command is $ source ll_env/bin/activate. To expire the virtual environment, use $ deactivate.
This command leads to below result:
3. Install Django with pip
The guide told me that I should use $pip install Django command directly to install the Django module. But I got below messages:
This message told me that I have no permission to install the package. Also, I will install this package into '/usr/local/lib/python2.7/dist-packages/django' but not the virtual environment.
If I check the pip version, I will got this result, It's also seems like I am not using the pip inside the virtual environment.
- Solution
I searched on the internet, think maybe I should use the pip executable file directly which is inside the virtual environment. But I can't find it under the bin folder. So the idea is to install the pip into the virtual environment first, then use this executable file to install any packages every time you need some modules in virtual environment.
First download the get-pip.py file to the virtual environment using $ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py.
The get-pip.py file is downloaded at current location. Use $ python get-pip.py to install pip.
The pip excutable files will be created under bin folder.
Type $ ./ll_env/bin/pip --version to check the pip installation path.
Finally, use $ ./ll_env/bin/pip install Django can intall Django in virtual environment successfully.
By doing below steps you can check if the Django module is installed successfully.