How to create a new Python project using PythonAnywhere, Bitbucket, SourceTree, and PyCharm

Background

  • I was using Windows 10 when following the steps below, so some things may be different on macOS / Linux.

The step-by-step process

  1. Create a new repository on Bitbucket / GitHub.
    1. Bitbucket:
      1. Go to https://bitbucket.org/
        • If you need to log in, get the login credentials from LastPass.
      2. Click the "+" icon in the top left corner and then click 'Repository'.  
      3. Fill out the 'Repository name' field.
      4. Click the 'Create repository' button.
      5. You'll be brought to a new webpage. 
      6. Select the URL in the top-right corner of the page. 
  2. Clone the new repository in PythonAnywhere.
    1. Go to http://pythonanywhere.com/.
    2. Create a new bash console. (Shortcut)
    3. Navigate to the directory you'd like this repository to live in.
    4. Type "git clone ", then paste the URL you copied from Bitbucket into the console (use Ctrl+v) and hit "Enter".
    5. Give it your Bitbucket password when it asks for it.
    6. (Optional:) To set up SSH access:
      1. From a PythonAnywhere bash console, run 'ssh-keygen'.
      2. You can hit just hit enter for all of the options it presents you (i.e. the path to the file and the password).
      3. Do a 'cat' on the id_rsa.pub file it creates to display the contents of the file.
      4. Copy the contents of the id_rsa.pub file and add a new SSH key entry for your Bitbucket account, name it 'PythonAnywhere', and paste in the contents of the id_rsa.pub file.
      5. If you did a 'git clone' and used a URL that begins with 'https', you need to change the URL to start with 'ssh' instead, otherwise it won't use your SSH key and will instead prompt you for your password:
        1. git remote set-url origin ssh://<your_username>@bitbucket.org/<your_username>/<name_of_repo>.git/
  3. Clone the new repository in SourceTree.
    1. Open SourceTree on your computer.
    2. Click 'File' → 'Clone / New' 
    3. A new tab will be created. 
    4. Paste the URL you copied from Bitbucket into the "Source Path / URL" field. 
    5. When you click out of the textbox, if you see an error about not having SSH keys... 
      1. Click "No" in the dialog to stop it from trying again. 
      2. Generate a public/private key pair with Puttygen.
        1. Click Tools → Create or Import SSH Keys 
        2. In the Putty Key Generator, click "Generate". 
        3. A bunch of text will appear in the modal. That's the public key.
        4. Fill out the "Key passphrase" and "Confirm passphrase" fields. 
        5. Click "Save private key". 
        6. Save the file to an easy-to-find directory and name it something memorable. 
        7. Click "Save public key" and name it something like "bitbucket_public_key". 
      3. Save the public key to your Bitbucket account.
        1. In Bitbucket, select your profile image in the bottom-left, and then click "Bitbucket settings". 
        2. In the Settings page, click "SSH keys". 
        3. Click "Add key". 
        4. Paste in the text from the Puttygen program and give it an easy-to-remember name. 
      4. Add the private key to Pageant on your computer.
        1. Right-click on Pageant from the Windows task tray and select "Add Key". 
        2. Select the file you saved earlier.
      5. Go back to SourceTree and try pasting in the URL again and then clicking out of the textbox, and you should see several new options (including the "Clone" button) show up. 
    6. Click "Clone".
  4. Create a PyCharm project in the new directory that SourceTree created on your hard drive.
    1. If an existing project opens up:
      1. Select File → New Project. 
    2. Otherwise, if the "Welcome to PyCharm" window appears... 
      1. Click "Create New Project".
    3. You'll be presented with a "New Project" window/modal.
    4. Fill out the fields of the modal.
      1. Location: Click the "..." button and navigate to the folder created by SourceTree. 
        1. You may need to click into the 'Documents' folder to see the name of the folder, e.g. C:\Users\Nathan\Documents\project_name.
      2. Interpreter: Ignore this / leave any value; we'll set the interpreter after creating the project.
    5. Click 'Create'. 
    6. It'll ask you if you want to create the project from existing sources. Click 'Yes'. 
  5. Create a virtualenv with PyCharm.
    1. With the project open, go to 'File' → 'Settings' 
    2. Navigate to "Project: project_name" → "Project Intepreter" 
    3. Click the gear icon in the top right and select "Add". 
    4. Fill out the fields in the modal that appears.
      1. Location:
        1. Navigate to the project folder. 
      2. Name: Once you've selected the 'Location' field, type 'venv' in the 'Name' field. This will create a new folder named 'venv' in the project folder. 
      3. Base interpreter: I'm guessing it's probably a good idea to set the base Python interpreter to be the same version as that used on PythonAnywhere.
        1. To get the version used on PythonAnywhere, I start a Python session. 
        2. To install that version of Python, just go to the main Python website. 
        3. When it's installing, I made a note of the directory it was installed to.
          1. Example: C:\Users\USERNAME\AppData\Local\Programs\Python\Python35\python.exe
        4. The 'AppData' installation directory didn't show up in PyCharm because it's hidden. 
        5. ...so I navigated to the directory in Windows Explorer and copied the path. 
        6. I then copied that path into PyCharm and added '/python.exe' to the end, and it found the file. 
      4. Inherit global site-packages: Leave this unchecked.
      5. Make available to all projects: I generally leave this unchecked.
    5. Click 'OK' in the 'Create Virtual Environment' modal.
    6. You should see a screen like the one in the image. 
    7. Click 'OK' to save the changes.
  6. (Windows only:) Set up PyCharm to start the virtualenv every time you start a Terminal session.
    1. (On Mac the venv is started automatically.  See JetBrains - PyCharm Help - Terminal)
    2. Background / Explanation
      1. We need to do this so that we can generate the 'requirements.txt' file (a later step).
    3. Step-by-step instructions:
      1. On the left-side folder-view pane, navigate to:
        1. Windows: 'your_project/venv/Scripts/activate.bat'. 
        2. Mac: 'your_project/venv/bin/activate'.
      2. Right-click the file, and select 'Copy Path'. 
      3. Go to File → Settings → Tools → Terminal → 'Shell path' and add '/K "path/to/activate.bat"' (with quotation marks around the path) after the "cmd.exe". Have "cmd.exe" in quotation marks as well.  (Source 1Source 2)
      4. Click "OK" to save your changes.
      5. Restart PyCharm.
      6. Start a Terminal session. 
      7. You should see the name of your virtualenv in parentheses on the command line. Running 'python' should start the version of python that you set earlier. 
  7. Create a 'requirements.txt' file in PyCharm.
    1. Explanation:
      1. We're doing this so that we can have our (not-yet-created) PythonAnywhere virtualenv download the necessary packages.
    2. Step-by-step instructions:  (You can find official instructions here.)
      1. Start a Terminal session and run 'pip freeze > requirements.txt'. (Source
        1. (As you can see in the screenshot, it may prompt you to upgrade pip first.)
          1. If you run into an error while upgrading, try running "easy_install -U pip".
      2. The 'requirements.txt' file should appear in the pane on the left.
        1. If you're creating a brand-new project, this file will probably be empty.
        2. If you're setting up a project after you've already written some code that uses certain packages, you should see a list of those packages and their versions. 
  8. Use SourceTree to push new files to the Bitbucket repository.
    1. If you switch back to SourceTree you should see a list of files that SourceTree has noticed as being new. 
    2. Ignore the '.idea/' folder and the 'venv' folder.
      1. We don't want to have our PyCharm files in the git repository (I think), so navigate to 'Actions' → 'Ignore' 
      1. Select 'Ignore everything beneath: .idea' 
      1. Once that's done those '.idea' files should disappear from the list of Unstaged files.
      2. Also ignore the venv folder.
        1. (These pictures were created before I knew to ignore the venv folder, so they show me including the venv folder to be committed. Don't do that.)
    3. Select the box next to 'Unstaged files' to select the remaining files to be committed. 
      1. (Note: Don't commit virtualenv folder/files to your repository; instead add them to the .gitignore file. These screenshots were taken before I knew that.) 
    4. Click the 'Commit' button. 
    5. Fill out some brief explanation of what you're committing and click the 'Commit' button. 
    6. Click the 'Push' button. 
    7. Select the 'Push?' box and click 'OK'. 
    8. You should see it switch to a progress bar. 
    9. Once it's finished, you can go to Bitbucket in your browser and confirm that the files were uploaded: 
  9. In PythonAnywhere, pull the new changes from Bitbucket.
    1. Open a bash console and switch into the project folder.
    2. Run 'git pull'. 
    3. It may prompt you for the Bitbucket password; it's in LastPass.
  10. Create a virtualenv in PythonAnywhere.
    1. Background
      1. After you do this, you'll have one virtualenv on your computer and one on PA.
    2. Step-by-step process
      1. Run the command mkvirtualenv name_of_your_virtualenv --python=/usr/bin/python3.5 (Source)
        1. Name the virtualenv after your project.
          1. Explanation: The command to activate a virtualenv on PythonAnywhere has global scope (is that the way to describe it?), so you can't name all the virtualenvs "venv"; you'll have a name conflict.
            1. So your virtualenv on your computer will be named "venv", and your virtualenv on PythonAnywhere will be named after your project.
        2. Change the version number at the end (e.g. python3.5) to the version of Python you'll be using.
          1. Note: It looks like specifying the minor version doesn't work. I tried "3.5.2" and it produced an error, but "3.5" worked.
  11. Install the package requirements in your PythonAnywhere virtualenv.
    1. Activate the virtualenv with workon name_of_your_virtualenv.
    2. Run pip install -r requirements.txt (Source
  12. In PyCharm, create the default project structure (Source)
    1. Default Python project folder structure
      project_name/  <-- Usually the same as your module name(?)
           MODULENAME/
               __init__.py
           bin/
      		.gitkeep
           docs/
      		.gitkeep
           setup.py
           tests/
               MODULENAME_tests.py
               __init__.py
    2. If you're creating a Flask / Vue.js project, you may want to use this alternative folder structure:

      Flask / Vue.js folder structure
      project_name/
          client/ <-- your Vue.js code will be kept in here
              .gitkeep
          server/ <-- your Flask code will be kept in here
              docs/
                  .gitkeep
              tests/
                  .gitkeep
              venv/
              requirements.txt
          .gitignore
          README.md
  13. (Optional) If you're trying to create a website, refer next to How to create a Flask web app using PyCharm and PythonAnywhere.