Virtual Environment Setup
With Pipenv
brew install pipenv
(Homebrew or Linuxbrew)- go to the root of your project folder and initialize pipenv
pipenv install
orpipenv install -r requirements.txt
, it will create pipfile (TOML syntax) and pipfile.lock; to install development and regular dependencies usepipenv install --dev
- use
pipenv install PACKAGE_NAME
to install most recent package version orpipenv install flask==0.12.1
to install exact package version orpipenv install -e git+https://github.com/requests/requests.git#egg=requests
to install versioned package - if you will add
--dev
to the install command (pipenv install flask --dev
) it will be installed only as development dependency - activate venv with
pipenv run shell
pipenv run python manage.py runserver
will start your local server orpipenv run python main.py
will run the code inside main.py- pipenv scripts - see in example below how you can easily shorten commands you use frequently
- pipfile should contains only main packages (Django, Requests, Pillow...) pipenv will automatically install their dependencies but will not include them in the pipfile but you can see them with command
pipenv graph
- to check vulnerabilities use
pipenv check
- to uninstall a package use
pipenv uninstall PACKAGE_NAME
- pipenv automaticaly loads variables from .env file (has to be also in the root directory)
## pipenv file example
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
ipdb = "==0.12"
[packages]
django-ckeditor = "==5.6.1"
django-extensions = "==2.1.6"
django-hosts = "==3.0"
gunicorn = "==19.9.0"
Django = "==2.2"
Pillow = "==7.0.0"
Pygments = "==2.3.1"
postgres = "*"
[requires]
python_version = "3.8"
[scripts]
runserver = "python manage.py runserver"
makemigrations = "python manage.py makemigrations"
migrate = "python manage.py migrate"
With Virtualenv Wrapper
- install virtualenv
sudo apt install python-virtualenv
- install virtualenv wrapper
pip install virtualenvwrapper
vim .bash_profile
and add these two lines on the end of fileexport WORKON_HOME=~/virtualenvs
(will create a dir virtualenvs in home dir where all the venvs will be stored) and
source /usr/local/bin/virtualenvwrapper.sh
- will create dir VENV_NAME in dir virtualenvs
# make venv with python 3 mkvirtualenv VENV_NAME --python=python3 # make venv with python 2 mkvirtualenv VENV_NAME --python=python2
workon VENV_NAME
(to activate venv VENV_NAME)deactivate
( to deactivate active venv)- install python packages you need or requirements.txt
pip install -r requirements.txt
Just Virtualenv
- create venv
# create venv with python 3 virtualenv -p python3 VENV_PATH # create venv with python 32 virtualenv VENV_PATH
- activate venv
source VENV_PATH/bin/activate
deactivate
( to deactivate active venv)- install python packages you need (
pip install django
for the newest version or for specific versionpip install Django==2.2
) or requirements.txtpip install -r requirements.txt
Resources:
➡️https://python-guide-cn.readthedocs.io/en/latest/dev/virtualenvs.html
➡️https://medium.com/the-andela-way/configuring-python-environment-with-virtualenvwrapper-8745c2895745
➡️https://virtualenvwrapper.readthedocs.io/
➡️https://www.djangoproject.com/download/