Skip to content

Instantly share code, notes, and snippets.

@tcd1558
Last active February 11, 2024 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tcd1558/a838deda7dd5968bab6c3d88b9aa466d to your computer and use it in GitHub Desktop.
Save tcd1558/a838deda7dd5968bab6c3d88b9aa466d to your computer and use it in GitHub Desktop.
PyCharm Python project with a GitHub repository

A PyCharm Python project with a GitHub repository

0. Overview

Here are the steps necessary to setting up a PyCharm Python project with a GitHub Repository. It is not obvious on what to do first: create a PyCharm project and add it to GitHub - or - create a new GitHub repository, clone it and convert it into a PyCharm Python project. Here is the approach I took. This text ist written from the perspective of MacOS.

P.S. PyCharm and IntelliJ are both from JetBrain. No wonder, the look and feel are very simular.

1. Index

  1. Overview
  2. Index
  3. Syntax
  4. Check your Python installalation
  5. Download PyCharm
  6. Create a Python Project in PyCharm
  7. Create a GitHub repository
  8. Prepare the Pycharm Python project to connect to GitHub
  9. Check for branch name conflicts
  10. Make changes to your local tree
  11. Create a Tag
  12. Project Management using GitHub and Pycharm

2. Syntax

[button] - is a button on the screen with the text 'button' on it.   
InputField: - is a field labeled 'InputField:', where you can enter text.   
<PlaceHolder> - a placeholder to be replaced with your own text (without the '<' and '>').  
[v] Label1 - ensure there is a check mark in front of the label 'Label1'
[x] Label2 - ensure there is an 'x' in the check box in front of the label 'Label2'
1/3 - step 1 of in total 3 steps
$ - prompt for a regular user
# - prompt for the root user (system administrator)

3. Check your Python installalation

It is always a good idea to make sure your installation is current. You do not want to encounter problems, which have already been fixed.
An even better approach is to create a virtual machine with e.g. docker and create your development environment from scratch with each project. In this way, you can also have different environments for different projects at the same time.

3.1 Update python

Is there a new python version?
Go to MD:python.org
HTML: python.org
Double click the download (Chrome browser: at the bottom margin of the browser). From a terminal, you can initiate the installation with the command

$ open python-3.9.5-macosx10.9.pkg

Follow the instructions to install python.

3.2 Update Anaconda

Check the version of the current installed conda software.

$ conda --version

Update the conda package manager to the latest version. To actually update, you will have to confirm a prompt.

$ conda update conda

Use conda to update Anaconda to the latest version.

$ conda update anaconda

See also https://docs.anaconda.com/anaconda/install/update-version/

3.3 How to verify what is installed

P.S. When using Anaconda, the python version might be slightly older than the publicly available python version.

Execute:

$ python3 --version

Notice: python can install packages with the pip/pip3 command, but python itself can not be updated using the pip/pip3 command.

And execute:

$ pip3 --version

If there is a newer version of pip available, execute

$ pip3 install --upgrade pip
$ pip install --upgrade pip

Other useful pip commands

$ pip3 list # List installed packages
$ pip3 show pip # Show information about installed packages
$ pip list --outdated # Show packages which have an upgrade at pypi

Update all packages installed with pip/pip3:

There is no dedicated command to get to the most current version. Instead you have to create a requirements list using the freeze option.

$ pip freeze > requirements.txt

Edit requirements.txt, and replace all ‘==’ with ‘>=’. Use the ‘Replace All’ command in the editor. Only lines with '==' are relevant. Lines not containing '==' are irrelevant.

$ cat requirements.txt | grep '==' | sed -e 's/==/>=/g' >requirements2.txt

pip install -r requirements2.txt --upgrade

P.S. If you encouunter problems, edit the requirements2.txt file and revert the '>=' back to '==' and rerun the pip install command.

Alternatively:
Open a new python program in PyCharm. If the package needed through import is missing, hover the mouse over the <pkg> name.

from pkg import function
e.g.
from selenium import webdriver

Hover over the 'pkg'. From the popup select 'Install package <pkg>'

3.4 homebrew update

$ brew --version

Update the brew package database

$ brew update

Check for outdated packages

$ brew outdated

3.5 Reboot the system

To make sure, the entire system is using the latest software, it is recommended to reboot your system.

4. Download PyCharm

[Jetbrains/pycharm][https://www.jetbrains.com/pycharm/download]
Community
Download .dmg(Intel)
Install from Chrome Downloads.

4.1 Update PyCharm

In the main bar click on Pycharm.
The select 'Check for. Updates...'

If there is an update available, you will see a notification at the bottom right once you open a project.

5. Create a Python Project in PyCharm

  1. Open PyCharm CE
  2. [New Project]
  3. Change Location: ~/PycharmProjects/<MyProject>
  4. [V] Create a main.py welcome script
  5. [Create]

6. Create a GitHub repository

  1. Go to the GitHub page of the team leader (If it is just you, you are the team leader):
    https://github.com/<team lead>
  2. In the top, black bar click on the + sign
  3. Select 'New repository'
  4. Match the name with the local project:

SeleniumEssentialTraining

  1. Provide a Description

A Pycharm Python project

  1. [v] Add a README file
  2. Click [Create repository]
  3. Capture the URL:
    https://github.com/<team lead>/<MyProject>
    Notice: There is a pull down button at the top left with a branch icon. This icon indicates, what name is used for the tree. This used to be 'master' and now 'main' is being used. Depending on the git version used on your system, there might be a conflict. Below are instructions on how to resolve this conflict.

7. Prepare the Pycharm Python project to connect to GitHub

7.1 Git init
7.2 Git Remote

7.1 Git init

7.1.1 Via a terminal
7.1.2 Via PyCharm

7.1.1 Via a terminal

$ cd ~/PycharmProjects/<MyProject>
$ git init

7.1.2 Via PyCharm

  1. In the top bar, select the VCS tab.
  2. Enable Version Control Integration...
  3. Select a version control system to associate with the project root: [Git]
  4. [OK]

7.2 Git remote

7.2.1 Via a terminal
7.2.2 Via PyCharm

7.2.1 Via a terminal

$ git remote add origin https://www.github.com/<team lead>/<MyProject>.git

When you made a mistake with above link, you can remove it with:

$ git remote remove origin

and display your setting with

$ git remote -v

Check the status of git with

$ git status

Please check what name is used for the tree. Look for 'On branch '

7.2.2 Via PyCharm

  1. Right click the project
  2. Select 'Git'
  3. Select 'Manage Remote...'
  4. Click on [+]
  5. Name: origin
  6. URL: https://github.com/<team lead>/<MyProject>.git
  7. [OK]
  8. If you made a mistake with the URL, an error message will pop up and you have a chance to correct your URL
  9. When the Name and URL have been added, click on [OK]

PyCharm does not have an equivalent to the '$ git status'. Changes to files are indicated by their color. See File Status Highlights for details on the meaning of the colors.

The branch name can be verified by the name in the bottom right margin, next to a lock. Depending on your git version it is either 'Y main' or 'Y master'

Other ways:

  1. Enter CMD+, or from the top bar, click on PyCharm, Preferences
  2. Version Control
  3. Git
  4. Protected branches: <name>

Or

  1. Right Click Project
  2. Git,
  3. Branches...
  4. Local Branches
  5. <name>

8. Check for branch name conflicts

In GitHub under https://github.com/<team lead>/<MyProject> in the tab <> Code, there is pulldown menu button to the top left of the page. This button indicates what tree you are currently viewing. By default this is the tree 'main'.

If the local name (e.g. master) and the remote name (e.g. main) do not match, you first have to add something to your local tree so you can give it a new name.

>>>Do not create the change here<<<

The project in GitHub is a GitHub repository and not a PyCharm project. The PyCharm Project is not a GitHub repository. By adding and committing the PyCharm files to the GitHub repository, you have all that is required to synchronize the branch names. The steps needed to synchronize your PyCharm project with the GitHub repository are documented in the next section.

9. Make changes to your local tree

When you enabled your PyCharm Project for GitHub (or # git init), a new .git directory with some configurations files were created. You can now add these files to your local staging area.

9.1 git add
9.2 set remote email and user name
9.3 git commit
9.4 Synchronizing the branch name
9.4.2 Verify the name change
9.5 git commit - name change
9.6 rebase
9.7 git push

9.1 git add

9.1.1 Via a terminal
9.1.2 Via PyCharm

9.1.1 Via a terminal

$ cd ~/PycharmProject/<MyProject>
$ git add .

9.1.2 Via Pycharm

Right click on the project <MyProject>
Git
Add or Option+CMD+a

9.2 set remote email and user name

9.2.1 Via a terminal
9.2.2 [Via PyCharm]

9.2.1 Via a terminal

Set configuration:

$ git config --global user.email "<mailuser@email.com>"
$ git config --global user.name "<GitHub user>"

Display configuration:

$ git config --list
$ git config --list --show-origin

9.2.2 Via PyCharm

  1. Sign out of GitHub
  2. Enter CMD+, or from the top bar, click on PyCharm, Preferences
  3.    [PyCharm], [Preferences], [Version Control], [GitHub], [+], [Login via GitHub...]
  4.    or [PyCharm], [Preferences], [Version Control], [GitHub], [Log in], [Login via GitHub...]
  5.    [PyCharm], [Preferences], [Appearance & Behavior], [System Settings], [Passwords]
  6. Do not save, forget passwords after restart
  7. Click [OK]

By setting the 'Do not save' option, you have the option of choosing which user you are going to use, if you have more than one: a team leader and a group member.

9.3 git commit

Commit means that the changes are confirmed into the local staging area. The changes will only be updated in the GitHub repository with the 'git push' command in one of the next following sections.

9.3.1 Via a terminal
9.3.2 Via PyCharm

9.3.1 Via a terminal

$ git commit -m "Initial Check-in to convert the GitHub repository into a PyCharm project and sync the tree name to 'main'"

9.3.2 Via PyCharm

Right click on the project <MyProject>
Git
Commit Directory...
A Changelist pops up
In the lower part of the Commit window, provide a Commit Message:
     "Initial Check-in to convert the GitHub repository into a PyCharm project and sync the tree name to 'main'"
Click [Commit]

9.4 Synchronizing the branch name

This is only necessary, when the remote branch and the local branch have different names.

9.4.1 Via a terminal
9.4.2 Via PyCharm

9.4.1 Via a terminal

$ git rename -M master main

9.4.2 Via PyCharm

Right click on the project <MyProject>
Git
Branches
Local Branches
Master > Rename...
Exchange 'master' with 'main'

Renaming the tree:
Left click on the tree in the bottom right margin.
Select <master>
Select Rename...
Give the tree its new name: main
Click [Rename]

9.4.2 Verify the name change

$ git status

or

bottom right margin

9.5 git commit - name change

$ git commit -m "branch name change"

You can not commit using PyCharm, because there is no file change.

9.6 rebase

9.6.1 Via a terminal
9.6.2 Via PyCharm

9.6.1 Via a terminal

$ git pull --rebase origin main

9.6.2 Via PyCharm

*** no satisfying solution for PyCharm yet ***

Not the same as CLI. Does not work. Right click on the project <MyProject>
Git
Rebase...
git rebase [branch or hash], pulldown
main
[Rebase]

Is this better?
Under git rebase
Click on [Modify options v]
Select another branch to rebase
origin/main main

9.7 git push

9.7.1 Via a terminal
9.7.2 Via PyCharm

9.7.1 Via a terminal

$ git push --set-upstream origin main
$ git push

9.7.2 Via PyCharm

Right Click on the project <MyProject>
Git
Push or Shift+CMD+K
main -> origin:main
[Push]

10. Create a Tag

See also Project Management using a PyCharm Python project with a GitHub repository.

Tag your branch, so you can always get back to this point of the repository.

10.1 Via a terminal
10.2 Via PyCharm

10.1 Via a terminal

$ git tag v1.0
$ git push origin v1.0

10.2 Via PyCharm

Right Click on the project <MyProject>
Git
New Tag...
Tag Name: -- here the letter v is allowed as first character. After that, you should have a sequence of numbers and the period. For example: 'v1.0'. A Tag can not have words like e.g. 'Winter2020/2021'.

Restrictions unclear: 'Commit:' is a reference for a commit Message can be verbous

Git, Push,
[v] Push Tags
Push

By default, the git push command doesn’t transfer tags to remote servers.

$ git push origin
https://git-scm.com/book/en/v2/Git-Basics-Tagging

PyCharm does always create a complete clone with all of its branches. Therefore when starting from the welcome screen with 'Get from Version Control' you will always get all branches, tags, ..
If you have to work on a specific branch/tag, you need to changes branch/tag on the bottom right margin of the screen.

I have not found a way to compare a file against a tagged version.

11. Project Management using GitHub and Pycharm

You can now make changes to your tree, but should if you are working on this tree with a team, you might want to look at: https://gist.github.com/tcd1558/e8491655c039330f9ac1570be5695b63

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment