create python project steps

來源:互聯網
上載者:User

標籤:help   phi   elf   virtual   for   spec   issue   and   too   

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 unittestclass 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 pythonraise 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 statusOn branch masterInitial commitChanges 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!

create python project steps

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.