Skip to content

Commit e4bc8de

Browse files
added note about gitHub repo
1 parent fc2a46a commit e4bc8de

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

Sources/source/where_to_put_your_code.rst

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
Where to put your custom code?
33
******************************
44

5+
6+
A Package Just for You!
7+
=======================
8+
59
(You can find this page at: http://bit.ly/JustPackage)
610

11+
.. note:: This page is generated from a Sphinx document managed in this gitHub repo: https://github.com/PythonCHB/PythonTopics. I welcome questions, comments, and, of course, Pull Requests.
12+
13+
714
A suggestion for how to manage your personal library of python functions you might use for scripting, data analysis, etc.
815

916

1017
TL; DR
1118
======
1219

13-
If you have a collection of your own code you want access to for various projects:
20+
If you have a collection of your own code you want to access for various projects:
1421

1522
Make a "package" out of it so you can manage it in one place, and use it in other places.
1623

@@ -20,21 +27,68 @@ You DO NOT NEED TO PUT IT ON PYPI !
2027
Introduction
2128
------------
2229

23-
Many scientists and engineers that do a little coding find they have a collection of little scripts and utilities that they want to be able to use and re-use for various projects. It is really NOT a good idea to simply copy and paste these around for use with each project -- you will regret that!
30+
Many scientists and engineers that do a little coding find they have a collection of little scripts and utilities that they want to be able to use and re-use for various projects.
31+
32+
Options for Handling Your Code Collection:
33+
------------------------------------------
34+
35+
1) Keep your code in one place, and copy and paste the functions you need into each new project.
36+
37+
38+
DON'T DO THAT!
39+
..............
40+
41+
REALLY!
42+
.......
43+
44+
45+
It is really NOT a good idea to simply copy and paste code around for use with each project. You will end up with multiple versions scattered all over the place -- **you will regret that!**
46+
47+
2) Put your code in a single directory and add it to the ``PYTHONPATH`` environment variable
48+
49+
50+
DON'T DO THAT!
51+
..............
52+
2453

25-
It is also not a good idea to use the ``PYTHONPATH`` environment variable to set up a directory in which to dump stuff. (Google a bit if you want to know why).
54+
REALLY!
55+
.......
56+
57+
``PTYHONPATH`` is shared by all installs of Python. What with Python2, Python3, virtual environments, etc -- it's really not a good idea.
58+
59+
If you don't believe me: **Google It**
60+
61+
62+
What you should do is make a "package"
63+
--------------------------------------
2664

2765
The best way to do this with Python is to use the Python package mechanism.
2866

2967
A Python "package" is a collection of modules and scripts -- we usually think of these as something carefully developed for a particular purpose and distributed to a wide audience for re-use -- the packages you can install with pip.
3068

3169
Indeed that is the case, but the "collection of modules and scripts" part can be used for your own code that no one else is ever going to touch, and the overhead is small if you use it only this way.
3270

71+
Why Don't People Tend to figure this out for themselves?
72+
........................................................
73+
74+
The Packaging Documentation is mostly about Making a "proper" package for distribution to a wide audience.
75+
76+
So newbies tend to either:
77+
78+
* Think: "I don't want/need to do all that", and then move on and copy and past their code around like they have already done.
79+
80+
or
81+
82+
* Go ahead and follow the instructions, and end up putting their tiny little not-useful-to-anyone-else package up on PyPi.
83+
84+
3385
The challenge is that most of the documentation about python packaging is focused on creating a package of a library that you want to distribute to the community. In that case, it's very important to have full and proper meta data, tests, documentation, etc. As a result, the packaging documentation makes the whole process seem complicated and cumbersome.
3486

35-
But making a simple package for your own use can be very simple, and very useful.
87+
Making a simple package just for your own use can be very simple, and very useful.
88+
..................................................................................
3689

37-
Step by step:
90+
91+
Step by Step:
3892
-------------
3993

4094
1) Create a directory in your user (or home, or ... ) dir for your code. Let's call it "my_code".
@@ -50,6 +104,7 @@ So you should have::
50104
my_code
51105
my_code
52106
__init__.py
107+
some_code.py
53108
setup.py
54109

55110
The inner my_code dir is now a python "package" -- any directory with a ``__init__.py`` file is a package. But how to use it?
@@ -68,6 +123,11 @@ But for now, we are going to make it as *simple* as possible::
68123

69124
That's it -- really! There is a lot you can do here to support multiple packages, scripts, etc, but this is enough to get you started.
70125

126+
Here: :download:`make_my_package.py <../code/make_my_package.py>` is a little script that will build an empty package for you::
127+
128+
python make_my_package.py your_package_name
129+
130+
will do it for you.
71131

72132
Putting in your code
73133
--------------------
@@ -95,7 +155,7 @@ OK -- now you have a (useless) package with some code in it - how to use it?
95155

96156
To use this package, you need to "install" it into the python environment that you want to use. Some of us have a single python install -- maybe Anaconda's root environment, or the python.org python installer, or ...
97157

98-
Some of us use virtualenv, or pipienv, or conda environments. In any case, get yourself into that environment at a command line and put yourself (``cd`` in Terminal, DOS box, etc...) in the outer my_code dir (where the setup.py is), and type::
158+
Some of us use virtualenv, or pipenv, or conda environments. In any case, get yourself into that environment at a command line and put yourself (``cd`` in Terminal, DOS box, etc...) in the outer my_code dir (where the setup.py is), and type::
99159

100160
pip install -e .
101161

@@ -107,15 +167,15 @@ Now you can fire up Python (or iPython, or a Jupyter notebook, or write code in
107167

108168
.. code-block:: ipython
109169
110-
In [2]: from my_code import some_code
170+
In [2]: from test_package import test_code
111171
112-
In [3]: some_code.test_fun()
172+
In [3]: test_code.test_fun()
113173
114174
yup -- this worked!!
115175
116176
And you are good to go!
117177

118-
Here is a zip file of my simple example package: :download:`my_code.zip <../code/my_code.zip>`
178+
.. Here is a zip file of my simple example package: :download:`my_code.zip <../code/my_code.zip>`
119179
120180
121181
NOTES:

0 commit comments

Comments
 (0)