There are open source Python packages for:
When a package relies on other software, we say that other software is a dependency of the package.
There are multiple ways to make a Python package.
The most common one is called a source distribution (a.k.a an sdist).
.py
files)setup.py
file (more on that in a sec...)All the files above are archived in a single file (a Tar or Zip archive usually)
setup.py
file
from setuptools import setup
setup(
name='cmsc-210',
version='1.0.0',
url='https://github.com/mazelife/cmsc-210',
author='James Stevenson',
author_email='author@gmail.com',
description='All code written in CMSC-210',
packages=["cmsc210"],
install_requires=['dependencyA == 1.11.1',
'dependencyB >= 1.5.0'],
)
setup.py
to a place where the
Python interpreter will find it.The python interpreter can tell you where it looks:
import sys
>>> print('\n'.join(sys.path))
/opt/anaconda3/envs/cmsc-210/lib/python38.zip
/opt/anaconda3/envs/cmsc-210/lib/python3.8
/opt/anaconda3/envs/cmsc-210/lib/python3.8/lib-dynload
/opt/anaconda3/envs/cmsc-210/lib/python3.8/site-packages
When using the Python distribution supplied with conda, site-packages
is where most things will go.
cmsc-210-1.0.0.tar.gz
If you have a source distribution, python can install it for you.
But where do you get a source distribution?
Let's take a look at an example.
To install a package from the package index, use a tool called pip in the terminal:
# Install the latest version of the "seaborn" package:
pip install seaborn
# Install a specific version of the "seaborn" package:
pip install seaborn==0.11.0
pip is:
You can also access pip through PyCharm if you don't want to use the terminal
The best way depends on the answer to this question:
Is my project, itself, a package that I want to share?
If your project is a package you are planning to share or publish to PyPI:
from setuptools import setup
setup(
name='cmsc-210',
version='1.0.0',
description='All code written in cmsc-210',
packages=['cmsc210'],
install_requires=['seaborn == 0.11.0',
'scikit-learn == 0.23' ],
)
If your project is just for you:
Create a requirements.txt
in the project root:
seaborn == 0.11.0
scikit-learn == 0.23
pip knows how to read these:
pip install -r requirements.txt
Setuptools also knows how to upload a package to PyPI:
# Only run this the first time you upload your package:
python setup.py register
# Push the current version to PyPI:
python setup.py upload
PyCharm will also ask you if you want to install the project's requirements when it sees these.
__init__.py
and all the rest if it's not.setup.py
setup.py
if you need one or requirements.txt
otherwise.Anaconda is a free and open-source Python platform that contains:
It's a good idea to create a Conda environment for each project you work on in PyCharm.
Let's use assignment #2 as an example...