Beginner Python – Creating Virtual Environment
For every project that I am working on, I create a virtual environment for it so that whatever libraries I have installed are distinct and separate from other project and system libraries.
Beginner Python – Creating Virtual Environment
- Folder for Projects
- Have a folder where you create all the projects you are working on
- Create a folder called Projects to have all of your project in one place
- Have a folder where you create all the projects you are working on
- Create a directory for Creating Virtual Environments
- Within the Projects folder, create a folder of your new project i.e. a car deals project has a folder called “autoprice”.
- Open up your terminal
- Change Directory (cd) and go within the project folder i.e. “autoprice”
- Input the following commands (You need to have virtualenv installed already)
- virtualenv nameofenv_env (i.e. virtualenv autoprice_env)
- This will install setup tools, and pip
- I just use _env as a self-reference that the folder is a virtual environment folder
- Important to note, do not mix-up folder of projects with folder of virtual environment. Virtual Environments are merely a way for you to separate out packages and dependencies projects to projects.
- Environments are not for your project files.
- Creating an Environment with a Specific Version of Python (2.6 for example)
- virtualenv -p /usr/local/bin/python2.6 env_name
- Activating the newly created Environment
- source nameofenv_env/bin/activate
- You should now be in your new environment
- Validating Which Environment You are In
- which python
- You should see the source be in the newly created environment directory
- which pip
- You should see the source be in the newly created environment directory
- pip list
- Only displays setup tools of the newly created virtualenv and non of the system tools
- Installing additional packages in the newly created Python Environment
- pip install lumpy
- pip install pytz
- pip install psutil
- You should the above packages in the newly created environment
- Exporting Packages and Their Version Numbers to another Environment / Project
- pip freeze –local > requirements.txt
- cat requirements.txt
- Go to the environment where you want to install all these packages
- pip install -r requirements.txt
- Exit Python Virtual Environment
- deactivate
- Delete Python Environment
- Deactivate Python Environment first
- rm -rf nameofenv_env
Read Learn to Code in Python to see how I got started with Python development