Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Child pages (Children Display)

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 'Repositories' and then the "+" icon in the top left corner and then click 'Repository'. Image Added Image Added
      3. Fill out the 'Repository name' field.
      4. Click the 'Create repository' button.
      5. Image Removed
      6. After you pick a name youYou'll be presented with brought to a new screenwebpage.
      7. Click "Clone".
        1. Image Removed
      8. Copy the command it shows you.
        1. Image Removed
      9. Switch to PythonAnywhere's website Image Added
      10. Select the URL in the top-right corner of the page. Image Added
  2. Clone the new repository in PythonAnywhere.
    1. Go to http://pythonanywhere.com/.
    2. Create a new bash console.
    3. If you're not already in the root directory, type "cd ~" and hit enter.
    4. Take the command you copied from Bitbucket and paste it into the console and hit "Enter".
    5. (Shortcut)
    6. Navigate to the directory you'd like this repository to live in.
    7. Type "git clone ", then paste the URL you copied from Bitbucket into the console (use Ctrl+v) and hit "Enter".
    8. Give it your Bitbucket password when it asks for it.
    9. (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 (it's software you keep on your computer).
    2. Click 'File' → 'Clone / New'
      1. Image Removed
      A dialog box will pop up. Click the button on the top-right to be shown a list of your online Bitbucket projects.
    3. Image Removed
    4. When you've selected it, the only thing you should have left to click is  Image Added
    5. A new tab will be created. Image Added
    6. Paste the URL you copied from Bitbucket into the "Source Path / URL" field. Image Added
    7. When you click out of the textbox, if you see an error about not having SSH keys... Image Added
      1. Click "No" in the dialog to stop it from trying again. Image Added
      2. Generate a public/private key pair with Puttygen.
        1. Click Tools → Create or Import SSH Keys Image Added
        2. In the Putty Key Generator, click "Generate". Image Added
        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. Image Added
        5. Click "Save private key". Image Added
        6. Save the file to an easy-to-find directory and name it something memorable. Image Added
        7. Click "Save public key" and name it something like "bitbucket_public_key". Image Added
      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". Image Added
        2. In the Settings page, click "SSH keys". Image Added
        3. Click "Add key". Image Added
        4. Paste in the text from the Puttygen program and give it an easy-to-remember name. Image Added
      4. Add the private key to Pageant on your computer.
        1. Right-click on Pageant from the Windows task tray and select "Add Key". Image Added
        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. Image Added
    8. 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. Image Added
    2. Otherwise, if the "Welcome to PyCharm" window appears.
    3. Image Removed
    4. Navigate to the folder.Image Removed.. Image Added
      1. Click "Create New Project".
    5. You'll be presented with a "New Project" window/modal.
    6. Fill out the fields of the modal.
      1. Location: Click the "..." button and navigate to the folder created by SourceTree. Image Added
        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.
    7. Click 'Create'. Image Added
    8. Image Removed
    9. It'll ask you if you want to create the project from existing sources. Click 'Yes'. Image Added
    10. Image Removed
  5. Create a virtualenv with PyCharm.
    1. With the project open, go to 'File' → 'Settings' Image Added
    2. Image Removed
    3. Navigate to "Project: project_name" → "Project Intepreter" Image Added
    4. Image Removed
    5. Click the gear icon in the top right and select "Create new virtualenvAdd". Image AddedImage Added
    6. Image RemovedImage Removed
    7. Fill out the "Location" and "Base interpreter" fieldsfields in the modal that appears.
      1. Location:
        1. Navigate to the project folder. Image AddedImage Added
        2. Image RemovedImage Removed
      2. Name: Once you've selected the project folder'Location' field, type 'venv' in the 'Name' field. This will create a new folder named 'venv' in the project folder.Image Removed Image Added
      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. Image AddedImage Added
        2. Image RemovedImage Removed
        3. To install that version of Python, just go to the main Python website. Image AddedImage Added
        4. Image RemovedImage Removed
        5. 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
        6. The 'AppData' installation directory didn't show up in PyCharm because it's hidden.Image Removed Image Added
        7. ...so I navigated to the directory in Windows Explorer and copied the path. Image AddedImage Added
        8. Image RemovedImage Removed
        9. I then copied that path into PyCharm and added '/python.exe' to the end, and it found the file.
          • Image Removed
      If it all works, you
        1.  Image Added
      1. Inherit global site-packages: Leave this unchecked.
      2. Make available to all projects: I generally leave this unchecked.
    8. Click 'OK' in the 'Create Virtual Environment' modal.
    9. You should see a screen like the one below. in the image. Image Added
    10. 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.
      • Image Removed
    Use SourceTree to push new files to the Bitbucket repository.
    1. Update: Don't commit your virtualenv folder/files to your repository. So add it to  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'. Image Added
        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. Image Added (Source 1Source 2)
      4. Click "OK" to save your changes.
      5. Restart PyCharm.
      6. Start a Terminal session. Image Added
      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. Image Added
  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'. (SourceImage Added
        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. Image Added
  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. Image Added
    2. Image Removed
    3. Ignore files/foldersIgnore 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'
      Image Removed
      1.  Image Added
      1. Select 'Ignore everything beneath: .idea' Image Added
        • Image Removed
      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.)
    4. Select the box next to 'Unstaged files' to select the remaining files to be committed. Image AddedImage Added
      1. (Note:
      the path for my venv files in the pics below isn't correct because I messed up the location of my venv the first time I created it. So ignore the paths you see in the screenshots.)Image RemovedImage Removed
      1. Don't commit virtualenv folder/files to your repository; instead add them to the .gitignore file. These screenshots were taken before I knew that.) 
    5. Click the 'Commit' button. Image Added
    6. Image Removed
    7. Fill out some brief explanation of what you're committing and click the 'Commit' button.Image Removed Image Added
    8. Click the 'Push' button. Image Added
    9. Image Removed
    10. Select the 'Push?' box and click 'OK'. Image Added
    11. Image Removed
    12. You should see it switch to a progress bar. Image Added
    13. Image Removed
    14. Once it's finished, you can go to Bitbucket in your browser and confirm that the files were uploaded: Image Added
    15. Image Removed
  9. In PythonAnywhere, pull the new changes from Bitbucket.
    1. Open a bash console and switch into the project folder.
    2. Run 'git pull'. Image Added
    3. Image Removed
    4. It 'll 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 (SourceImage Added
  12. In PyCharm, create the default project structure (Source)

    1. Code Block
      titleDefault Python project folder structure
      collapsetrue
      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:

      Code Block
      titleFlask / Vue.js folder structure
      collapsetrue
      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.