create python project steps

Setting Up Your First Project

You don't have to manually create the structure above, many tools will help you build this environment. For example the Cookiecutter project will help you manage project templates and quickly build them. The spinx-quickstart command will generate your documentation directory. Github will add the README.md and LICENSE.txt stubs. Finally, pip freeze will generate the requirements.txt file.

Starting a Python project is a ritual, however, so I will take you through my process for starting one. Light a candle, roll up your sleeves, and get a coffee. It's time.

  1. Inside of your Projects directory, create a directory for your workspace (project). Let's pretend that we're building a project that will generate a social network from emails, we'll call it "emailgraph."

    $ mkdir ~/Projects/emailgraph
    $ cd ~/Projects/emailgraph
    
  2. Initialize your repository with Git.

    $ git init
    
  3. Initialize your virtualenv with virtualenv wrapper.

    $ mkvirtualenv -a $(pwd) emailgraph
    

    This will create the virtual environment in ~/.virtualenvs/emailgraph and automatically activate it for you. At any time and at any place on the command line, you can issue the workon emailgraph command and you'll be taken to your project directory (the -a flag specifies that this is the project directory for this virtualenv).

  4. Create the various directories that you'll require:

    (emailgraph)$ mkdir bin tests emailgraph docs fixtures
    

    And then create the various files that are 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
    
  5. Generate the documentation using sphinx-quickstart:

    (emailgraph)$ sphinx-quickstart
    

    You can safely use the defaults, but make sure that you do accept the Makefile at the end to quickly and easily generate the documentation. This should create an index.rst and conf.py file in your docs directory.

  6. Install nose and coverage to begin your test harness:

    (emailgraph)$ pip install nose coverage
    
  7. Open up the tests/__init__.py file with your favorite editor, and add the following initialization tests:

    import unittest
    
    class InitializationTests(unittest.TestCase):
    
        def test_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("Was 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=emailgraph \
                  --cover-inclusive --cover-erase tests
    

    You should see two tests passing along with a 100% test coverage report.

  8. Open up the setup.py file and add the following lines:

    #!/usr/bin/env python
    raise NotImplementedError("Setup not implemented yet.")
    

    Setting up your app for deployment is the topic of another post, but this will alert other developers to the fact that you haven't gotten around to it yet.

  9. Create the requirements.txt file using pip freeze:

    (emailgraph)$ pip freeze > requirements.txt
    
  10. Finally, commit all the work you've done to email graph to the repository.

    (emailgraph)$ git add --all
    (emailgraph)$ git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   LICENSE.txt
        new file:   README.md
        new file:   bin/emailgraph-admin.py
        new file:   docs/Makefile
        new file:   docs/conf.py
        new file:   docs/index.rst
        new file:   emailgraph/__init__.py
        new file:   requirements.txt
        new file:   setup.py
        new file:   tests/__init__.py
    
    (emailgraph)$ git commit -m "Initial repository setup"
    

With that you should have your project all setup and ready to go. Get some more coffee, it's time to start work!

posted @ 2017-12-05 16:50  牛哥学coding  阅读(215)  评论(0编辑  收藏  举报