Setting up Your First Project
You don ' t has to manually create the structure above, many tools would help you build this environment. For example the Cookiecutter project would help you manage project templates and quickly build them. The Spinx-quickstart command would generate your documentation directory. Github would add the README.md
and LICENSE.txt
stubs. Finally, would pip freeze
generate the requirements.txt
file.
Starting a Python project is a ritual, however, so I'll take your through my process for starting one. Light a candle, roll up your sleeves, and get a coffee. It ' s time.
Inside of your Projects directory, create a directory for your workspace (project). Let's pretend that we ' re building a project that would generate a social network from emails, we'll call it "emailgraph."
$ mkdir ~/projects/emailgraphcd ~/projects/emailgraph
Initialize your repository with Git.
$ git init
-
Initialize your virtualenv with virtualenv wrapper.
$ mkvirtualenv-a $ (pwd
This would create the virtual environment in ~/.virtualenvs/emailgraph and automatically Activate it for you. At no time and at no place on the command line, you can issue the workon emailgraph
command and you'll be t Aken to your project directory (the -a
flag specifies that's the project directory for this virtualenv) .
-
Create The various directories that you ll require:
(emailgraph) $ mkdir Bin tests emailgraph Docs fixtures
And then create the various files that is needed:
(emailgraph) $ Touch Tests/__init__.py (emailgraph$ Touch emailgraph/__init__.py (emailgraph ) $ touch setup.py readme.md LICENSE.txt. Gitignore (Emailgraph) $ Touch bin/emailgraph-admin.py
Generate the documentation using sphinx-quickstart
:
(Emailgraph)$ sphinx-quickstart
You can safely with the defaults, but make sure so do accept the Makefile on the end to quickly and easily generate T He documentation. This should create a Index.rst and conf.py file in your docs
directory.
Install nose and coverage to begin your test harness:
(Emailgraph)$ pip install nose coverage
Open tests/__init__.py
up the file with your favorite editor, and add the following initialization tests:
ImportUnitTestClassInitializationtests(UnitTest.TestCase):DefTest_initialization(Self):"""Check the test suite runs by affirming 2+2=4 "" "self. Assertequal (2+2 4) def test_import ( self "" " Ensure the test suite can import our Module "" "try: import Emailgraph except importerror: self fail ( "is not able to import the emailgraph" )
From your project directory, you can now run the test suite with coverage as follows:
(Emailgraph)$ nosetests-v--with-coverage--cover-package --cover-inclusive--cover-erase tests
You should see the tests passing along with a 100% test coverage report.
Open up the setup.py
file and add the following lines:
#!/usr/bin/env pythonnotimplementederror("Setup not implemented yet." )
Setting up your apps for deployment are the topic of another post, but this would alert other developers to the fact it you Haven ' t gotten around to it yet.
-
Create The requirements.txt
file using pip freeze
:
( Emailgraph) $ pip freeze > Requirements.txt
-
Finally, commit all of the work you've done to email graph to the repository.
(emailgraph) $ git add--all (Emailgraph) $ git Statuson branch masterinitial commitchanges to being committed: (use span class= "s2" > "git rm--cached <file> ..." to Unstage) new file:LICENSE.txt new file:README.md NE W file:bin/emailgraph-admin.py New File:docs/makefile new file:docs/conf.py new File:docs/index.rst new File:emailgra ph/__init__.py New File:requirements.txt New file:setup.py new File:tests/__init__.py (emailgraph< Span class= "O" >) $ git commit-m "Initial Repository Setup"
With this you should has your project all setup and ready to go. Get Some more coffee, it's time to start work!
Create Python project steps