Sunday, November 29, 2020

Python Virtual Environment (venv)

 Python Virtual Environment (venv)


Pre-Req :

  • This is on Mac
  • Make sure Python3 is installed in the system
  • $pip3 install virtualenv #--upgrade
  • $virtualenv #help

Steps:

  1. create folder
  2. cd inside folderName
  3. $virtualenv env_name  #-p /folder_path_of_python_to_clone/bin/python
  4. $source ./env_name/bin/activate
    1. #if u get permission denied "$chmod -R 777 *"
    2. #Note u can also have multiple environments and activate which ever u want
  5. Notice "env_name" in the Terminal line meaning it is activated.
  6. Try:
    1. $python
    2. import requests # you will get error
    3. exit()
  7. Try2 (make sure virtual env is still activated)
    1. $pip install requests
    2. $python
    3. import requests
    4. exit()
  8. $pip freeze > requirements.txt
  9. To install requirements from above file #pip install -r requirements.txt
  10. $deactivate #Deactivate the env

Note:

  1. To use Virtual Environment in Pycharm
  2. Preferences> Settings > Add > Virtualenv 
  3. Make sure it is pointing to venv

No comments:

Post a Comment