Python Package Best Practices
(./[Link]): Setup
This setup tutorial will walk you through installing the software you will need for this workshop.
For this workshop, you will need to have Python installed. We recommend and assume you will have Python
installed using Anaconda, and will be talking about package management using conda and Anaconda (see
instructions below). You will also need to download workshop materials, and configure Git.
We will cover the following topics. Click on a particular topic to skip to that section.
1. Downloading workshop materials
2. Installing Python using Anaconda
3. Downloading a text editor
4. Creating a Python environment using conda
5. Installation of cookiecutter
6. Installing and Configuring Git
7. Creating a GitHub Account
Workshop Preparation
In this workshop, we will be moving code from a Jupyter notebook into a Python package that we can install and
import into other scripts.
Click here to download the starting materials for the workshop (/python-package-best-
practices/data/starting_material.zip)
Create a folder on your desktop called molssi_best_practices .
The file you download as starting materials will be a zip file. You should unzip this file into the folder you
have created on your desktop. After downloading and unzipping, verify that you see the following directory
structure.
molssi_best_practices
└── starting_material
├── data
│ ├── pdb
│ │ ├── [Link]
│ │ ├── [Link]
│ │ └── [Link]
│ └── xyz
│ ├── [Link]
│ ├── [Link]
│ └── [Link]
└── starting_notebook.ipynb
Installing Python Using Anaconda
If you already have a Python 3 version Anaconda or MiniConda installed, you can skip this step.
Python ([Link] is a popular language for scientific computing, and great for general-purpose
programming as well. We recommend using Python with the conda package manager. You can obtain Python and
conda by either installing Miniconda ([Link] (minimal installer) or
Anaconda ([Link] Anaconda comes bundled with many more Python
packages than miniconda. Anaconda is more user-friendly. If you are uncomfortable navigating the terminal, or
are unsure of which you need, install Anaconda.
The installer for Anaconda can be found on this page ([Link] Choose
the appropriate version for your Operating System.
Throughout the rest of this set-up, we will assume that you are using the conda package manager.
Please set up your Python environment at as far in advance as possible. If you encounter problems with the
installation procedure, ask your workshop organizers via e-mail for assistance, so you are ready to go as soon as
the workshop begins.
Windows Subsytem for Linux (WSL)
If you plan to continue developing in the future and you utilize the Windows Operating system, you may want
to consider installing the WSL. The WSL will allow you to run a Linux based terminal from your Windows
machine, which can aid in the development and running of code. The WSL can connect to VS code for easy
development. You can find instructions to install the WSL on the Windows Documentation
([Link]
Choosing a text editor
You will need an editor for Python files for this workshop. If you do not have a preferred text editor, we
recommend Visual Studio Code ([Link] If you installed a recent version of Anaconda,
you will have VSCode installed already. You should be able to open it from the Anaconda Navigator window. If you
have another preferred text editor, you should use that for the workshop.
Using Anaconda and conda
Use of Anaconda ([Link] with its package manager, conda , greatly
simplifies package installation and environment management.
conda is a general package manager, meaning that it can install dependencies and packages in languages
besides Python, unlike pip (which is Python’s package manager). Both pip and conda can be used to install
packages.
Python environments
A conda environment contains a specific collection of packages you have installed. This means that packages
are isolated, and installed only for a specific environment – you can have several environments each with different
installed packages, or different versions of installed packages in different environments.
It’s considered a best practice to create a new Python environment for each project you work on.
This section uses the command line interface (CLI) to create an environment using conda . If you are on Mac or
Linux, you will type these commands into your terminal. If you are on Windows, you should use the Anaconda
Prompt.
1. Create environment & activate it
To create an environment for this project using conda ,
Bash
$ conda create --name molssi_best_practices python=3.9
For other projects, you should replace molssi_best_practices with a descriptive name for your project.
conda also allows you to specify the Python version to use with the environment. Here, python=3.9 specifies
that we want to use Python 3.9 in this environment. Executing this command will list the environment location and
a list of Python packages to be installed. Choose y(es) when prompted.
Activate the environment using the command
Bash
$ conda activate molssi_best_practices
Once you’ve activated an environment, the name of the environment will be in parentheses at the front of your
command line prompt.
If you wanted to create an environment for testing your code in Python 3.6, for example, you could use the
command (Do not execute this, it’s just an example.)
Bash
$ conda create --name molssi_36 python=3.6
When this environment is activated, Python 3.6 will be used instead of Python 3.9.
To see a list of all your environments
Bash
$ conda info --envs
To deactivate an environment, type
Bash
$ conda deactivate
Package installation using conda
Using conda , we can install packages to our environments. Note: Make sure you have activated the
environment where you want to install packages.
Bash
$ conda activate molssi_best_practices
To list all the Python packages installed in an environment, first activate it, then type
2. Install packages
Bash
$ conda list
Packages can be installed using the conda install package_name command. For example, to install NumPy
(do not execute this, we will install NumPy later),
Bash
$ conda install numpy
Further, the desired version of NumPy can be specified:
Bash
$ conda install numpy=1.15
For this workshop, you will need to install the following packages into your environment
NumPy
Matplotlib
jupyter notebook
Packages available to Conda are stored within channels . Some packages are not stored in the default Conda
channel, so we need to specify where Conda can find the package with -c followed by a channel name in our
installation command. The NumPy and Matplotlib packages are both stored within the default Conda channel,
so we do not need to include a -c . The jupyter notebook package is in the conda-forge channel, so we
include the syntax -c conda-forge in our installation command.
Bash
$ conda install numpy matplotlib ⼀一般channel
$ conda install -c conda-forge notebook 特殊channel
CookieCutter Installation
For this workshop, we will create the structure of our Python package using the CMS CookieCutter
([Link] Please have this package installed in your
molssi_best_practices environment.
First, switch to your environment for this workshop if you are not in it.
Bash
$ conda activate molssi_best_practices
Install the general cookiecutter with the following commands.
Bash 3. Install general cookecutter
$ conda config --add channels conda-forge
$ conda install cookiecutter
Installing and configuring Git
Installation
Install Git. You can install using conda
Bash 4. Install git
$ conda install -c conda-forge git
Configuring Git Configuration
The first time you use Git on a particular computer, you need to configure some things.
First, you should set your identity. One of the most important things that version control like Git does is to keep
track of who changes what. This helps repository maintainers coordinate the efforts of all the people who
contribute to the project. Most importantly, it makes it easier to figure out who to blame when something goes
wrong. You can provide Git your name and contact information with the following commands:
Bash
$ git config --global [Link] "YOUR NAME"
$ git config --global [Link] "YOUR_EMAIL"
You should next set the default branch name you will work on. Branches will be explained in detail later. This
configuration setting makes the default branch name match the default branch name on GitHub
Bash
git config --global [Link] main
Next, you might want to change the Git text editor. As we will see later, certain Git commands will open text files.
When this happens, Git will use your environment’s default text editor, which might not be the editor you are most
comfortable using. Using configuration commands, you can tell Git to use your favorite editor.
A popular chose is Vim. To use Vim, do
Bash
$ git config --global [Link] "vim"
A more complete list of possible editors is available here ([Link]
setup/[Link]).
To use VSCode as your core editor, do
Bash
$ git config --global [Link] "code --wait"
You can check the configuration commands that you have set using:
Bash
$ git config --list
Create GitHub Account
Create an account on [Link] ([Link] if you do not have one already. Remember the username
and password. If you are making a GitHub account, please remember that your username should be recognizable
and professional.
The next step is to verify your email address through GitHub. If you do not verify your email address, you will not
be able to perform many of the actions covered in this workshop.
Accessing GitHub through the Command Line
To utilize GitHub from the command line, you will need to provide GitHub with an SSH public key
([Link] An SSH key is a
means to authenticate remote servers. With an SSH key, you will be able to access your GitHub repositories
through the command line without the need to enter a username and password. The linked documentation
provides information about SSH keys, how to find out if you already have an SSH key, and how to generate and
add one to your GitHub account. You should click the link and follow the instructions to add an SSH key to your
GitHub account.
Conclusion
At the end of this set-up, you should have created a Python environment ( molssi_best_practices ) which has
Python 3.9, numpy , matplotlib , jupyter , and cookiecutter installed. You should also have downloaded
starting material, installed and created an account on GitHub, and configured Git.
Copyright © 2018–2021 The Molecular Sciences Software Institute ([Link]
Edit on GitHub ([Link]
pages/[Link]) / Contributing ([Link]
practices/blob/gh-pages/[Link]) / Source ([Link]
package-best-practices/) / Cite ([Link]
practices/blob/gh-pages/CITATION) / Contact ([Link]
Using a MolSSI modification of The Carpentries style ([Link] version 9.5.0
([Link]
!"#$%& ( !)*# +, "#$%
.*/#* / !"#$%& *&012%&3*&# 4 3%566178*6#7,2/.#1.*6 9 :$1.$ $/6 !"#$%& ;<=
>.%&?/ /.#10/#* 3%566178*6#7,2/.#1.*6
!" !"#$"%&# #'()" *(+,"-- !"#$"%&.("/&01&2*+*'(.(3/4&2
我的condo运⾏行行有问题安装这个就好了了
! "#$%& '$()&** $+,-. ,&)-*#)*'/
[Link]
! "#$%& '$()&** 0" "#$%&01#234 $#)4/##5 36659453/intel-mkl-fatalerror-cannot-
!"#$%& '$()&** ,-*6)##*5')( load-libmkl-avx2-so-or-libmkl-def-so
conda install -c conda-forge git
!" -&"5&34 ()#24% '$ %41&+*) "#$%& "7&$$4*8 $conda install nomkl bumpy
$conda remove mil mil-service
!" 6/0* #$%& .$/&&*5E 'C. .%&?/C.$/&&*57&/3* ,/.@/A*7&/3*
F%&?/ /&? ,1, /2* 8%#$ ,/.@/A* 3/&/A*2E #$*" ()*+ ,/.@/A* ,-./01&
*&012%&3*&#< 20 ,2%G*.# 345'*+& *&012%&3*
-. 7 .%&?/ .2*/#* 8&/3* D*:7,2%G*.# ,"#$%&B;<=
9: 7 .%&?/ /.#10/#* D*17,2%G*.#
;< 7 .%&?/ ?*/.#10/#*
4H.#10/#* /0 *&012%&3*&# =>.%33/&? ?@3ABCD#& *&012%&3*&# &/3*9
EFG! *&07 .%&?/ 1&D% C C*&06
EFG! ,/.@/A*7 .%&?/ 516#
——————
设置 default 的 text editor :
vim:
$ git config --global [Link] "vim"
VSCode:
$git config --global [Link] "code --wait"
在 terminal 打开 VSCode: code .
在 terminal 用 VSCode 打开一个 file: code [Link]
设置 path