{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 05wk-2: 모듈, 패키지, 라이브러리\n", "\n", "최규빈 \n", "2023-04-05\n", "\n", "\n", "\n", "# 강의영상\n", "\n", "> youtube:\n", "> \n", "\n", "# intro\n", "\n", "`-` 현재 파이썬은 길이가 2인 벡터의 덧셈을 지원하지 않음" ], "id": "8b0d568d-bb4a-4a32-9aa3-93e2b79975bf" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "a=[1,2]\n", "b=[3,4]\n", "a+b" ], "id": "ffc98a50-faf6-4302-9749-62ce4358f994" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 아래와 같은 기능을 구현하는 함수를 만들고 싶음\n", "\n", "\\[1,2\\], \\[3,4\\] -\\> \\[4,6\\]\n", "\n", "`-` 구현" ], "id": "01b57b1b-4f19-4d87-b18b-967386be776f" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def vec2_add(a,b): \n", " return [a[0]+b[0], a[1]+b[1]]" ], "id": "8a7458f9-84e8-4f5d-8a53-782ddc528f95" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` test" ], "id": "a22d71b6-f351-47e7-aa57-ce1889eea257" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "a=[1,2]\n", "b=[3,4]" ], "id": "b42f8db7-d34b-4aaa-9371-84bff28ecfad" }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "vec2_add(a,b)" ], "id": "893fa0b6-84f0-4b6d-ba8e-baa4a5995b44" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# make `myfuns.py`\n", "\n", "`-` 생각해보니까 vec2_add는 내가 앞으로 자주 쓸 기능임\n", "\n", "`-` 그런데 현재 사용방법으로는 내가 노트북파일을 새로 만들때마다\n", "`def vec2_add(a,b):` 와 같은 형태로 vec2_add를 매번 정의해줘야 하는\n", "불편한이 있다.\n", "\n", "## 해결1\n", "\n", "`-` 자주 사용하는 함수를 `myfuns.py`에 저장한다.\n", "\n", "``` python\n", "# myfuns.py\n", "def vec2_add(a,b): \n", " return [a[0]+b[0], a[1]+b[1]]\n", "```\n", "\n", "`-` %run myfuns를 실행\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "a9167a2f-1551-4326-b98f-f9822933bef5" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%run myfuns " ], "id": "2ff0cfd2-b853-4573-870d-b85d4d4b659c" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "vec2_add([1,2],[3,4])" ], "id": "f983744c-216b-4842-ab33-e148db82eb67" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 해결2\n", "\n", "`-` 자주 사용하는 함수를 `myfuns.py`에 저장한다.\n", "\n", "``` python\n", "# myfuns.py\n", "def vec2_add(a,b): \n", " return [a[0]+b[0], a[1]+b[1]]\n", "```\n", "\n", "`-` import myfuns를 이용\n", "\n", "(준비) “00” -\\> 커널재시작" ], "id": "8ecc397e-ba62-4613-87ae-a61774ed2348" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import myfuns " ], "id": "2c889d49-848d-417e-8243-f02d5173dfb0" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "a=[1,2]\n", "b=[3,4]\n", "myfuns.vec2_add(a,b)" ], "id": "49463a2a-ca2b-4186-a772-d93d3fc49e87" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# import 기본\n", "\n", "## 사용방법\n", "\n", "`-` 사용방법1\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "30541f1d-e8cb-40d4-a0e9-51b9618ef59f" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns " ], "id": "0ef31d20-11a1-4c4e-9b51-7a87d4bea3c2" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "myfuns.vec2_add([1,2],[3,4]) " ], "id": "7f7a04a1-5d3c-4f23-803c-44760e185ded" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- myfuns.vec2_add 의 의미: myfuns.py 라는 파일안에 vec2_add라는 함수가\n", " 있음. 그것을 실행하라.\n", "- `.`의 의미: `상위.하위`의 개념!\n", "\n", "(주의) 아래와 같이 사용불가능 하다." ], "id": "288cae05-bfd4-4d2d-b65b-35d1553bd1d7" }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [], "source": [ "vec2_add([1,2],[3,4])" ], "id": "f4675030-e9b3-4a12-b643-536c547f5c21" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 사용방법2\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "d3a5c87d-8e66-4929-ba91-e654a3bd3b6a" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from myfuns import vec2_add " ], "id": "bd9c7d32-987a-4dec-af79-4d0a46b71892" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "vec2_add([1,2],[3,4])" ], "id": "d3a8ba6d-c2ad-488f-9709-f79952277b0e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(주의) 이 경우는 오히려 아래가 불가능함" ], "id": "241c98c2-30ec-4b52-ab18-8c71072a2c30" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "myfuns.vec2_add([1,2],[3,4]) # myfuns안의 vec2_add만 임포트했지 myfuns자체를 임포트 한것은 아님 " ], "id": "4ea0f9b1-b779-473b-92de-367b425eefdc" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 사용방법3\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "6a962a69-51e8-4a44-8931-62cd626b7134" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns\n", "from myfuns import vec2_add" ], "id": "2b3ba15d-bab0-4d23-a6a9-c2d38fb36bd5" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "myfuns.vec2_add([1,2],[3,4])" ], "id": "816239fc-0399-418d-a932-d56279bbfe67" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "vec2_add([1,2],[3,4])" ], "id": "0b3f4901-da62-4d73-a10e-9abb4975152a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 사용방법4\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "50326b10-19f4-451d-9fa8-61bfc7c7b4ec" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from myfuns import vec2_add, vec2_sub " ], "id": "2d800976-d839-478c-8951-e27734477915" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "vec2_add([1,2],[3,4])" ], "id": "92dfa064-0b3f-47fe-8a08-0ff8125235ce" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "vec2_sub([1,2],[3,4])" ], "id": "6d35ef9f-7dd6-4ac4-bdf6-11c2e9bfa267" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 사용방법5\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "4cc30e8f-b5ab-4297-8f03-4c8fa3c14f3b" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from myfuns import * #*는 all의 의미 " ], "id": "9a2717f3-9f4f-449c-9acb-b22d652ee43a" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "vec2_add([1,2],[3,4])" ], "id": "009985a1-4c98-4929-a4e4-5c1dfe984761" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "vec2_sub([1,2],[3,4])" ], "id": "21bbb950-52dd-44c7-927f-0146b3eccf2e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 사용방법6\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "39a9a5b1-6a20-4104-8a9d-eb64eab0593c" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns as mf " ], "id": "217811c3-083f-4767-95c3-339773495523" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mf.vec2_add([1,2],[3,4])" ], "id": "097463ca-d044-4d4f-bec1-1277b7f17247" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "mf.vec2_sub([1,2],[3,4])" ], "id": "21f777ae-3d20-425d-95ec-08ebbf3c57dd" }, { "cell_type": "markdown", "metadata": {}, "source": [ "(오히려 아래는 실행불가능)" ], "id": "2ed70ff0-6bda-4897-a1e9-9fabb2a37142" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "myfuns.vec2_add([1,2],[3,4])" ], "id": "aaf67901-1deb-4c45-b564-9ae5e1af678e" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "myfuns.vec2_sub([1,2],[3,4])" ], "id": "eb906459-7b6c-4bd9-8302-e7ee34cb675a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 잘못된 사용방법1\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "e00c8199-ad1e-4a28-9552-bbb36196bb58" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns as mf \n", "from mf import vec2_add " ], "id": "0743e6b4-e077-4f6c-b80d-c3ce79fbd115" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 사용방법7\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "9d83b6aa-f15d-4da9-9eec-266be0ec23e2" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns as mf \n", "from myfuns import vec2_add " ], "id": "5fcce038-c801-4db3-93e0-941ccf5ac6a1" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "mf.vec2_add([1,2],[3,4])" ], "id": "5c019c12-bdc4-4272-8df1-db2b4f733493" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "vec2_add([1,2],[3,4])" ], "id": "4b1a4ec2-1940-49d1-a545-b06c7bf9262f" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 사용방법8\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "634c8df6-cd09-4f8d-b571-609ac3ea324e" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns as mf \n", "from myfuns import vec2_add as add " ], "id": "8d62be0f-53c6-4825-a2ab-8561b84d64e3" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mf.vec2_add([1,2],[3,4])" ], "id": "01fca27f-f1a5-4649-9f3f-0d3841243836" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "vec2_add([1,2],[3,4])" ], "id": "0e8598fa-92c2-4112-a145-b663ddb9eddc" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "add([1,2],[3,4])" ], "id": "748eb343-66fa-49c0-94a9-0abb1b280861" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 도움말 작성기능\n", "\n", "`-` mf란 무엇인가?\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "ba6b2132-dc8a-493d-aff5-ac63a6ee70ec" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns as mf " ], "id": "545d529c-cc33-4151-820d-2d7127722f95" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mf" ], "id": "5bef3b5c-0fdc-464a-867f-891d61e9d7fd" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "mf?" ], "id": "6f75b592-6270-4427-bd8c-6b753dc877af" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "type(mf)" ], "id": "3d055de8-daac-410b-96c2-754db9ab4dbb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- mf의 타입은 모듈이라고 나옴, 현재 단계에서는 무엇인지 알기 어려움\n", "\n", "`-` Docstring의 내용을 채울 수 있을까?\n", "\n", "준비1: myfuns.py 파일을 아래와 같이 수정한다.\n", "\n", "준비2: “00” -\\> 커널재시작" ], "id": "c5800e6a-49cc-4e48-90f3-2fd30da17a97" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns as mf " ], "id": "49832bca-3ffc-4a7d-a80e-c0bce929b9b3" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "mf?" ], "id": "c8db7ac6-bf5d-4cb9-a549-75595bfc5d5d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 주의점\n", "\n", "`-` `myfuns.py`는 최초 한번만 import 된다.\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "5566a638-2fa0-4a3a-a57e-5125aad97fb6" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns" ], "id": "acfe65a9-f53b-46b7-9494-af76e03b8954" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "myfuns.vec2_add([1,2],[3,4])" ], "id": "8722fd99-5b7f-4fe9-93d3-e0090fcca82a" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`myfuns.py`파일을 열고 함수를 아래와 같이 바꾸자.\n", "\n", "``` python\n", "\"\"\"이것은 길이가 2인 벡터의 합 혹은 차를 구하는 모듈입니다.\"\"\" \n", "def vec2_add(a,b): \n", " print(\"이것은 myfuns.py에 정의된 함수입니다\") \n", " return [a[0]+b[0], a[1]+b[1]]\n", "def vec2_sub(a,b): \n", " return [a[0]-b[0], a[1]-b[1]]\n", "```\n", "\n", "다시 myfuns를 로드하고 myfuns.vec2_add 를 실행하여 보자." ], "id": "50107434-18c2-4ba3-9912-150ff66d5390" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import myfuns" ], "id": "ba07d5f9-e9f9-4c52-9708-23cf12a8b827" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "myfuns.vec2_add([1,2],[3,4])" ], "id": "6435f7ab-7a46-485c-93b0-e0ba68c26cc8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "바뀐내용이 적용되지 않는다.\n", "\n", "커널을 다시 시작하고 임포트해보자.\n", "\n", "“00” -\\> 커널재시작" ], "id": "557a37e9-ae47-4802-aa49-ae7569dc5418" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import myfuns" ], "id": "bed6da2b-e897-4101-bc3d-e19f56a854fb" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이것은 myfuns.py에 정의된 함수입니다" ] } ], "source": [ "myfuns.vec2_add([1,2],[3,4])" ], "id": "73a73a21-ffbf-47f1-9473-eb50a19f650c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` `myfuns.py`는 주피터노트북파일과 같은 폴더에 존재해야 한다.\n", "\n", "준비1: “00” -\\> 커널재시작\n", "\n", "준비2: `myfuns.py`을 복사하여 다른 폴더로 이동. 예를들면 IP0403 폴더를\n", "만들고 그 폴더안에 myfuns.py파일을 복사해서 붙여넣은뒤에 파일이름을\n", "myfuns2.py 로 변경." ], "id": "e74c378a-e870-41c3-a824-2196ac8a53a5" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import myfuns # 주피터노트북파일과 같은 폴더에 있는 myfuns는 잘 로드되지만 " ], "id": "a1d40149-b665-4176-b506-d0b471894f67" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import myfuns2 # 주피터노트북파일과 다른 폴더에 있는 myfuns2는 그렇지 않다. " ], "id": "165420e4-a661-4d20-89eb-2e3121460460" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` IP0403 폴더에 있는 myfuns2.py를 실행하기 위해서는 아래와 같이 할 수\n", "있다.\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "396f09f5-6755-48f8-92e5-84d23b6c6f3a" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from IP0403 import myfuns2" ], "id": "9f57caca-6965-4a73-be1c-99cd7e568abd" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이것은 myfuns2.py에 정의된 함수입니다" ] } ], "source": [ "myfuns2.vec2_add([1,2],[3,4]) " ], "id": "4c013c7f-d4d4-421a-99a5-dd1d478f5fa7" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 아래도 가능하다.\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "fda10d2f-b7a7-407b-b0c0-408c996066d3" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from IP0403.myfuns2 import vec2_add as add " ], "id": "faf6c053-2978-4f72-afae-f5796965daf5" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이것은 myfuns2.py에 정의된 함수입니다" ] } ], "source": [ "add([1,2],[3,4])" ], "id": "a543ad80-0a41-4fb5-917a-7b8451de0625" }, { "cell_type": "markdown", "metadata": {}, "source": [ "참고로 아래는 모두 정의되지 않음" ], "id": "3dd1fc65-3711-4d8a-95c7-d824b35fcfeb" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "IP0403.myfuns2.vec2_add([1,2],[3,4]) " ], "id": "1bb0d58c-153c-4983-84a4-87ed0785445d" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "myfuns2.vec2_add([1,2],[3,4]) " ], "id": "bd98263b-6b0a-47ff-a0ed-608507e7bae1" }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "vec2_add([1,2],[3,4]) " ], "id": "44dc14a3-bace-43be-b24a-40b838a135f7" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# import 고급\n", "\n", "## 폴더와 함께 사용할시\n", "\n", "`-` 언뜻 생각하면 아래가 가능할 것 같다.\n", "\n", "``` python\n", "import IP0403 \n", "IP0403.myfuns2.vec2_add([1,2],[3,4]) \n", "```\n", "\n", "`-` 하지만 불가능하다.\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "25b71ba5-c042-4daa-8940-bf95b5a48a1a" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import IP0403 " ], "id": "4b327bc5-c22c-4796-88c9-be5773c212e5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 되는거아냐?" ], "id": "78eb2256-15d3-4549-a306-55ddd8bfb615" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "IP0403.myfuns2.vec2_add([1,2],[3,4])" ], "id": "0a0f80fc-ef95-4a6c-a555-2dd3ebfd2db8" }, { "cell_type": "markdown", "metadata": {}, "source": [ "- 여기서 불가능하다.\n", "\n", "`-` **(암기)** IP0403 폴더안에 `__init__.py`라는 파일을 만들고 내용에\n", "아래와 같이 쓰면 가능하다.\n", "\n", "``` python\n", "# ./IP0403/__init__.py \n", "from . import myfuns2\n", "```\n", "\n", "준비1: 위의 지침을 따른다.\n", "\n", "준비2: “00” -\\> 커널재시작" ], "id": "b36b3ab9-f933-4f02-a339-fac2eb6d4952" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import IP0403 " ], "id": "052a9b76-a579-4da9-b859-e2c1057a4883" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "이것은 myfuns2.py에 정의된 함수입니다" ] } ], "source": [ "IP0403.myfuns2.vec2_add([1,2],[3,4])" ], "id": "f5866a18-6654-4471-b371-0c885823473e" }, { "cell_type": "markdown", "metadata": {}, "source": [ "**컴퓨터 상식** - `.`: 현재폴더를 의미 - `..`: 상위폴더를 의미 -\n", "`./myfuns.py`: 현재폴더안에 있는 myfuns.py를 의미 -\n", "`./IP0403/myfuns2.py`: 현재폴더만에 IP0403폴더안의 myfuns2.py 파일을\n", "의미 - `../myfuns.py`: 현재폴더보다 한단계상위폴더에 있는 myfuns.py를\n", "의미 - `cd ./IP0403`: 현재폴더안에 있는 IP0403폴더로 이동해라.\n", "(`cd IP0403`으로 줄여쓸 수 있음) - `cd ..` 현재폴더보다 한단계\n", "상위폴더로 이동하라.\n", "\n", "**따라서 `from . import myfuns2`는 현재폴더에서 myfuns2를 찾아서 임포트\n", "하라는 의미로 해석가능**\n", "\n", "`-` 의미상으로 보면 아래가 실행가능할듯 한데 불가능하다." ], "id": "f033b33d-b8c8-49c7-aa80-e1cee9d7c8f6" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#import myfuns\n", "from . import myfuns" ], "id": "f1992b93-6dc2-46bb-86e3-ea0046add92c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "## site-packages (실습금지)\n", "\n", "`-` 의문: 왜 현재폴더에 numpy.py라든가 numpy라는 이름의 폴더가 없는데도\n", "import 가능한지?\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "c77604de-22b6-43e1-ac06-5cb8be41ac20" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ], "id": "468f0cb3-832a-4422-8045-20a03090505c" }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import IP0403 as ip " ], "id": "0840bd40-5fb3-49a5-914a-32c915339669" }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "ip?" ], "id": "ec310a73-1776-4a85-86b4-9d990debe8ce" }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "np?" ], "id": "35192748-2d19-4507-82bd-b7b76385596c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 추측: `~/anaconda3/envs/py310/lib/python3.10/site-packages/`를\n", "찾아가보자. 그곳에 numpy폴더가 있을 것이다." ], "id": "33547d6d-4ca5-4bc7-a6bf-3a23ca1e2bc5" }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "numpy\n", "numpy-1.22.2.dist-info" ] } ], "source": [ "!ls ~/anaconda3/envs/py310/lib/python3.10/site-packages | grep numpy" ], "id": "ceaaaf68-5731-4919-a811-d3b3dfc3ab2d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 추측2: `~/anaconda3/envs/py310/lib/python3.10/site-packages/`에 내가\n", "자주 쓰는 기능을 폴더로 만들어서 모아두면 어디서든지 import 할 수 있다." ], "id": "97d851f6-bea3-4cfd-aa38-7e6a786bd118" }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "!mkdir ~/anaconda3/envs/py310/lib/python3.10/site-packages/guebin # guebin 폴더 생성 " ], "id": "fc3bcab1-9e3d-405b-8a87-5dd8008876eb" }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "!cp ./myfuns.py ~/anaconda3/envs/py310/lib/python3.10/site-packages/guebin \n", "# 현폴더에 있는 myfuns.py를 아까만든 guebin 폴더로 복사 " ], "id": "bcabf2fe-7e33-4420-95b1-f58a5af80742" }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "from guebin import myfuns" ], "id": "d3b9d818-ef74-43bd-9dd6-0af505d4a1b2" }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "myfuns?" ], "id": "2e174e4d-dcff-4239-9f92-e5b1827bd828" }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "!rm ~/anaconda3/envs/py310/lib/python3.10/site-packages/guebin -rf # guebin 폴더삭제 " ], "id": "7acb63b0-fb63-4d05-abbf-e42f2d1b5939" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 추측3: guebin이 사라진 상태에서는 `from guebin import myfuns` 이\n", "동작하지 않을 것이다.\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "2e167c24-b5e2-4ac5-b257-a77be131d0b2" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from guebin import myfuns" ], "id": "333e805b-1106-4981-9f93-da7e548cb92d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 추측4: `~/anaconda3/envs/py310/lib/python3.10/site-packages/`에서\n", "numpy를 지운다면 numpy를 import할 수 없다.\n", "\n", "준비: “00” -\\> 커널재시작" ], "id": "86b337c9-953c-48fb-9e58-2f76009bc02f" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ], "id": "5e93e14a-5a47-4d17-9b5e-4b46e5eadbcb" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` 추측5: `!pip install numpy`를 하면 다시 폴더가 생길 것이다." ], "id": "a83fe32c-f35f-4131-8e3a-57adf4aa4e40" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Found existing installation: numpy 1.22.2\n", "Uninstalling numpy-1.22.2:\n", " Successfully uninstalled numpy-1.22.2" ] } ], "source": [ "!pip uninstall numpy -y " ], "id": "3bf3e7a2-8ebd-49aa-8b59-f7094efa6fc0" }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Collecting numpy\n", " Downloading numpy-1.22.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.8 MB)\n", " |████████████████████████████████| 16.8 MB 11.4 MB/s eta 0:00:01\n", "Installing collected packages: numpy\n", "Successfully installed numpy-1.22.3" ] } ], "source": [ "!pip install numpy " ], "id": "1bc9331e-158d-487a-b1fe-45cd212d51ad" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 모듈, 패키지, 라이브러리?\n", "\n", "`-` 모듈의 개념은 아까 살펴본것과 같다. (import를 하여 생기게 되는\n", "오브젝트)\n", "\n", "`-` 교수님들: 모듈이 모이면 패키지라고 부른다. 그리고 라이브러리는\n", "패키지보다 큰 개념이다.\n", "\n", "`-` 그런데 구분이 모호하다." ], "id": "26b550fe-5b26-4d8f-a44b-ff2cede6937c" }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ], "id": "d0bade15-350a-4553-a4c3-3754ffda1dee" }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "type(np)" ], "id": "0971cf4b-f7a7-4962-a393-cc50c92d6e52" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`-` python에서의 numpy의 type은 모듈\n", "\n", "`-` 그런데 numpy package 라고 검색하면 검색이 된다.\n", "\n", "`-` 심지어 numpy library 라고 해도 검색가능\n", "\n", "`-` 내생각: 넘파이모듈, 넘파이패키지, 넘파이라이브러리 다 맞는 말임" ], "id": "fb5c51d3-8cfc-4c31-8561-22bca26870a6" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3", "language": "python" }, "language_info": { "name": "python", "codemirror_mode": { "name": "ipython", "version": "3" }, "file_extension": ".py", "mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.16" } } }