Skip to content

Instantly share code, notes, and snippets.

@Yogendra0Sharma
Created January 12, 2017 10:51
Show Gist options
  • Save Yogendra0Sharma/4060fa0350daf2e5df63cd7569e649a5 to your computer and use it in GitHub Desktop.
Save Yogendra0Sharma/4060fa0350daf2e5df63cd7569e649a5 to your computer and use it in GitHub Desktop.
Howto Setup a Python Virtual Environment

Python Virtual Environment Howto

##SETUP VIRTUALENV on Ubuntu Server

console: ~ $ sudo apt-get install python-virtualenv
console: ~ $ virtualenv python #Careful where you call this command as it creates a folder in the current directory
console: ~ $ source ./python/bin/activate
console: ~ $ deactivate
  • IF YOU WANT TO DELETE A VIRTUALENV JUST DELETE THE FOLDER

##SETUP VIRTUALWRAPPER On Ubuntu Server

console: ~ $ sudo apt-get install python-pip
console: ~ $ cd <dir> #NAVIGATE: to the folder where you want your virtualenv installed
console: ~ $ sudo pip install virtualenvwrapper 
console: ~ $ export WORKON_HOME=~/Envs 
console: ~ $ source /usr/local/bin/virtualenvwrapper.sh
console: ~ $ mkvirtualenv envdjango0001
console: ~ $ workon envdjango0001
console: ~ $ deactivate
console: ~ $ rmvirtualenv envdjango0001 # can reuse this name again afterwards

##Other Useful Commands

  • lsvirtualenv - List all of the environments.
  • cdvirtualenv - Navigate into the directory of the currently activated virtual environment, so you can browse its site-packages, #for example.
  • cdsitepackages - Like the above, but directly into site-packages directory.
  • lssitepackages - Shows contents of site-packages directory.

##Activating a Virtualenvwrapper env after it has been setup and you have logged in and out again

console: ~ $ export WORKON_HOME=~/Envs 
console: ~ $ source /usr/local/bin/virtualenvwrapper.sh
console: ~ $ workon envdjango0001

##OK NOW FOR THE CHANGE IN TACK! I was thinking of using virtualenvwrapper until Jeff Knupp
rightly pointed out that in order to produce a fully reproducible
environment that is under source control (git) we should consider placing
the virtualenv and the django project in the same folder.

##THIS SECTION ONLY APPLIES TO WHEN ONE IS CREATING A PROJECT FOR THE FIRST TIME.

  • ALL THESE FILES ARE PART OF THE TRADE PROJECT IN THE GIT REPO! ###Virtualenv for our django project
console: ~/Projects $ mkdir tradeproject
console: ~/Projects $ cd tradeproject
console: ~/Projects/tradeproject $ virtualenv envtrade
console: ~/Projects/tradeproject $ source ./envtrade/bin/activate

###Installing Django

  • Ensure you are running in a virtualenv as described above
(envtrade)console: ~/Projects/tradeproject $ pip install django # installs the latest version of django
(envtrade)console: ~/Projects/tradeproject $ which django-admin.py # to check you are using the virtualenv version of django
/home/user/Projects/tradeproject/envtrade/bin/django-admin.py #Should show area under env directory. If not, check if see (envtrade) at prompt. If not, you forgot to run
# (envtrade)console: ~/Projects/tradeproject $ source ./envtrade/bin/activate

###Creating Django Project

(envtrade)console: ~/Projects/tradeproject $  django-admin.py startproject djangotrade #where "djangotrade" is the name of the django project

###Setup Git And Pull Project from Github

(envtrade)console: ~/Projects/tradeproject $ git init #After this command ensure ssh keys are setup...
(envtrade)console: ~/Projects/tradeproject $ git remote add origin git@github.com:tripattern/trade.git
(envtrade)console: ~/Projects/tradeproject $ git pull origin master
(envtrade)console: ~/Projects/tradeproject $ git config --global user.email "systems@tripattern.com"
(envtrade)console: ~/Projects/tradeproject $ git config --global user.name "Tripattern"
(envtrade)console: ~/Projects/tradeproject $ git commit -a -m 'Initial commit of myproject'
(envtrade)console: ~/Projects/tradeproject $ git push -u origin master

###Install South - Helps with handling changes in the models and database...

(envtrade)console: ~/Projects/tradeproject $ pip install south
(envtrade)console: ~/Projects/tradeproject $ vim djangotrade/djangotrade/settings.py 
Start Edit
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'south',                                                                                                                                                     
)
End Edit
(envtrade)console: ~/Projects/tradeproject $ git add .
(envtrade)console: ~/Projects/tradeproject $ git commit -a -m 'Added south'
(envtrade)console: ~/Projects/tradeproject $ git push -u origin master

##IF YOU ARE PULLING THE PROJECT FROM ONLINE... ###Setup Git And Pull Project from Github

console: ~/Projects $ mkdir tradeproject
console: ~/Projects $ cd tradeproject
console: ~/Projects/tradeproject $ git init #Ensure ssh keys are setup...
console: ~/Projects/tradeproject $ git remote add origin git@github.com:tripattern/trade.git
console: ~/Projects/tradeproject $ git pull origin master
console: ~/Projects/tradeproject $ source ./envtrade/bin/activate

#NOTES

  • I have created two folders:
    • tradeproject #This is the main, production repo
    • trdprjdev0001 #This is a branch called tradedev0001 on the Github repo
  • Commit new files
    • git add README.md # only for new files...
    • git commit -m "first commit"
    • git push -u origin
  • Update file on remote repo
    • git commit -a -m "Replaced incorrect semi-colon"
    • git push -u origin
  • Pull Changes from github
    • git pull origin # updates local files

##REFERENCES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment