You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/source/where_to_put_your_code.rst
+69-9Lines changed: 69 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,22 @@
2
2
Where to put your custom code?
3
3
******************************
4
4
5
+
6
+
A Package Just for You!
7
+
=======================
8
+
5
9
(You can find this page at: http://bit.ly/JustPackage)
6
10
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
+
7
14
A suggestion for how to manage your personal library of python functions you might use for scripting, data analysis, etc.
8
15
9
16
10
17
TL; DR
11
18
======
12
19
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:
14
21
15
22
Make a "package" out of it so you can manage it in one place, and use it in other places.
16
23
@@ -20,21 +27,68 @@ You DO NOT NEED TO PUT IT ON PYPI !
20
27
Introduction
21
28
------------
22
29
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
+
24
53
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
+
--------------------------------------
26
64
27
65
The best way to do this with Python is to use the Python package mechanism.
28
66
29
67
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.
30
68
31
69
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.
32
70
71
+
Why Don't People Tend to figure this out for themselves?
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
+
33
85
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.
34
86
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.
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::
50
104
my_code
51
105
my_code
52
106
__init__.py
107
+
some_code.py
53
108
setup.py
54
109
55
110
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::
68
123
69
124
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.
70
125
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.
71
131
72
132
Putting in your code
73
133
--------------------
@@ -95,7 +155,7 @@ OK -- now you have a (useless) package with some code in it - how to use it?
95
155
96
156
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 ...
97
157
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::
99
159
100
160
pip install -e .
101
161
@@ -107,15 +167,15 @@ Now you can fire up Python (or iPython, or a Jupyter notebook, or write code in
107
167
108
168
.. code-block:: ipython
109
169
110
-
In [2]: from my_code import some_code
170
+
In [2]: from test_package import test_code
111
171
112
-
In [3]: some_code.test_fun()
172
+
In [3]: test_code.test_fun()
113
173
114
174
yup -- this worked!!
115
175
116
176
And you are good to go!
117
177
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>`
0 commit comments