여러 파이썬 파일을 하나의 파이썬 파일로 만들기
하고 싶은 일
예를 들어, main.py
가 다른 파일을 호출하는 프로젝트를 ...
- main.py
- sub1.py
- folder
- sub2.py
↓↓↓ 의존하고 있는 함수나 클래스 등을 제대로...
onefile.py
규! ! ! 어서.
(동기적으로는 프로젝트 관리 파이썬 파일을 google colab에서 쉽게 실행할 수 없을 것이라고 생각했기 때문입니다.)
해결책
여기 stickytape라는 파이썬 패키지를 사용하면 괜찮습니다.
htps : // py 피. 오 rg / p 로지 ct / s 치키 타페 /
stickytape = 접착 테이프
실험
실험 한 코드는 모두 Github에 공개되어 있으므로 참고로.
install
이제 stickytape 명령을 사용할 수 있습니다. (명령 이름은 ...)
$ pip install stickytape
파일 준비
다음과 같이 구성하십시오.
- main.py
- sub1.py
- folder
- sub2.py
folder/sub2.py
적절하게 Apple 클래스를 만들고 적절하게 value 속성을 가질 수 있습니다.
class Apple:
def __init__(self, value):
self.value = value
b1. py
적절하게 평균 함수를 만들 때.
sub1.pydef mean(a, b):
return (a+b)/2
마인. py
가져 와서 적절하게 계산하고 적절하게 표시합니다.
from sub1 import mean
from folder.sub2 import Apple
apple1 = Apple(value=100)
apple2 = Apple(value=200)
result = mean(apple1.value, apple2.value)
print(result)
막상! 하나의 파일에!
다음 명령을 실행하십시오. (물론 onefile.py는 무엇이든 괜찮습니다)
$ stickytape main.py > onefile.py
결과
다음과 같은 onefile.py
가 생성됩니다.
#!/usr/bin/env python
import contextlib as __stickytape_contextlib
@__stickytape_contextlib.contextmanager
def __stickytape_temporary_dir():
import tempfile
import shutil
dir_path = tempfile.mkdtemp()
try:
yield dir_path
finally:
shutil.rmtree(dir_path)
with __stickytape_temporary_dir() as __stickytape_working_dir:
def __stickytape_write_module(path, contents):
import os, os.path
def make_package(path):
parts = path.split("/")
partial_path = __stickytape_working_dir
for part in parts:
partial_path = os.path.join(partial_path, part)
if not os.path.exists(partial_path):
os.mkdir(partial_path)
open(os.path.join(partial_path, "__init__.py"), "w").write("\n")
make_package(os.path.dirname(path))
full_path = os.path.join(__stickytape_working_dir, path)
with open(full_path, "w") as module_file:
module_file.write(contents)
import sys as __stickytape_sys
__stickytape_sys.path.insert(0, __stickytape_working_dir)
__stickytape_write_module('sub1.py', 'def mean(a, b):\n return (a+b)/2')
__stickytape_write_module('folder/sub2.py', 'class Apple:\n\n def __init__(self, value):\n self.value = value')
from sub1 import mean
from folder.sub2 import Apple
apple1 = Apple(value=100)
apple2 = Apple(value=200)
result = mean(apple1.value, apple2.value)
print(result)
한순간 「뭐야!?」가 되었습니다만, 이것을 실행하면...
150.0
성공적인 계산 결과가 표시되었습니다.
Google Colab에서 실험
방금 코드를 Google Colab에 복사하여 실행했습니다.
아래와 같이 무사히 150.0이 표시되었습니다.
(무지개 빛깔의 고양이가 걷는 것을 걱정하지 마십시오.)
스크립팅
여기에서 여담입니다.
stickytape
라는 명령은 길고 일단 생성 파일의 디렉토리를 지정하거나 게을리하지 않으므로 다음과 같이 스크립팅하는 것이 좋습니다.
- main.py
- sub1.py
- folder
- sub2.py
- scripts
- tape.sh
- build
- onefile.py
tape.sh# 初期値
entry="main.py"
output="onefile.py"
# オプション
while getopts e:o: OPT
do
case $OPT in
"e" ) entry=${OPTARG};;
"o" ) output=${OPTARG};;
esac
done
# 実行
stickytape ${entry} > "build/${output}"
다음 명령은 main.py를 실행하여 빌드 디렉토리에 onefile.py를 생성합니다.
$ sh scripts/tape.sh
옵션도 준비했습니다.
옵션 이름
설명
-e
진입점 파일 이름
-o
출력할 파일 이름
$ sh scripts/tape.sh -e <ファイル名> -o <ファイル名>
생성 된 디렉토리는 build
로 고정되어 있으므로 싫다면 마음대로 변경하십시오.
자기소개
서두에 쓰면 방해가 되므로 마지막에 몰래 자기 소개시켜 주세요.
이름
코토오카 아키키
학교
고베 대학 대학원
학부 연구
기계 학습, 음성 처리
대학원 연구
기계 학습, 공정성, 생성 모델, etc
트위터
@Wataoka_Koki
트위터를 따르십시오!
Reference
이 문제에 관하여(여러 파이썬 파일을 하나의 파이썬 파일로 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/wataoka/items/dfc67048c7f214a31265
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
- main.py
- sub1.py
- folder
- sub2.py
onefile.py
여기 stickytape라는 파이썬 패키지를 사용하면 괜찮습니다.
htps : // py 피. 오 rg / p 로지 ct / s 치키 타페 /
stickytape = 접착 테이프
실험
실험 한 코드는 모두 Github에 공개되어 있으므로 참고로.
install
이제 stickytape 명령을 사용할 수 있습니다. (명령 이름은 ...)
$ pip install stickytape
파일 준비
다음과 같이 구성하십시오.
- main.py
- sub1.py
- folder
- sub2.py
folder/sub2.py
적절하게 Apple 클래스를 만들고 적절하게 value 속성을 가질 수 있습니다.
class Apple:
def __init__(self, value):
self.value = value
b1. py
적절하게 평균 함수를 만들 때.
sub1.pydef mean(a, b):
return (a+b)/2
마인. py
가져 와서 적절하게 계산하고 적절하게 표시합니다.
from sub1 import mean
from folder.sub2 import Apple
apple1 = Apple(value=100)
apple2 = Apple(value=200)
result = mean(apple1.value, apple2.value)
print(result)
막상! 하나의 파일에!
다음 명령을 실행하십시오. (물론 onefile.py는 무엇이든 괜찮습니다)
$ stickytape main.py > onefile.py
결과
다음과 같은 onefile.py
가 생성됩니다.
#!/usr/bin/env python
import contextlib as __stickytape_contextlib
@__stickytape_contextlib.contextmanager
def __stickytape_temporary_dir():
import tempfile
import shutil
dir_path = tempfile.mkdtemp()
try:
yield dir_path
finally:
shutil.rmtree(dir_path)
with __stickytape_temporary_dir() as __stickytape_working_dir:
def __stickytape_write_module(path, contents):
import os, os.path
def make_package(path):
parts = path.split("/")
partial_path = __stickytape_working_dir
for part in parts:
partial_path = os.path.join(partial_path, part)
if not os.path.exists(partial_path):
os.mkdir(partial_path)
open(os.path.join(partial_path, "__init__.py"), "w").write("\n")
make_package(os.path.dirname(path))
full_path = os.path.join(__stickytape_working_dir, path)
with open(full_path, "w") as module_file:
module_file.write(contents)
import sys as __stickytape_sys
__stickytape_sys.path.insert(0, __stickytape_working_dir)
__stickytape_write_module('sub1.py', 'def mean(a, b):\n return (a+b)/2')
__stickytape_write_module('folder/sub2.py', 'class Apple:\n\n def __init__(self, value):\n self.value = value')
from sub1 import mean
from folder.sub2 import Apple
apple1 = Apple(value=100)
apple2 = Apple(value=200)
result = mean(apple1.value, apple2.value)
print(result)
한순간 「뭐야!?」가 되었습니다만, 이것을 실행하면...
150.0
성공적인 계산 결과가 표시되었습니다.
Google Colab에서 실험
방금 코드를 Google Colab에 복사하여 실행했습니다.
아래와 같이 무사히 150.0이 표시되었습니다.
(무지개 빛깔의 고양이가 걷는 것을 걱정하지 마십시오.)
스크립팅
여기에서 여담입니다.
stickytape
라는 명령은 길고 일단 생성 파일의 디렉토리를 지정하거나 게을리하지 않으므로 다음과 같이 스크립팅하는 것이 좋습니다.
- main.py
- sub1.py
- folder
- sub2.py
- scripts
- tape.sh
- build
- onefile.py
tape.sh# 初期値
entry="main.py"
output="onefile.py"
# オプション
while getopts e:o: OPT
do
case $OPT in
"e" ) entry=${OPTARG};;
"o" ) output=${OPTARG};;
esac
done
# 実行
stickytape ${entry} > "build/${output}"
다음 명령은 main.py를 실행하여 빌드 디렉토리에 onefile.py를 생성합니다.
$ sh scripts/tape.sh
옵션도 준비했습니다.
옵션 이름
설명
-e
진입점 파일 이름
-o
출력할 파일 이름
$ sh scripts/tape.sh -e <ファイル名> -o <ファイル名>
생성 된 디렉토리는 build
로 고정되어 있으므로 싫다면 마음대로 변경하십시오.
자기소개
서두에 쓰면 방해가 되므로 마지막에 몰래 자기 소개시켜 주세요.
이름
코토오카 아키키
학교
고베 대학 대학원
학부 연구
기계 학습, 음성 처리
대학원 연구
기계 학습, 공정성, 생성 모델, etc
트위터
@Wataoka_Koki
트위터를 따르십시오!
Reference
이 문제에 관하여(여러 파이썬 파일을 하나의 파이썬 파일로 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/wataoka/items/dfc67048c7f214a31265
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
$ pip install stickytape
- main.py
- sub1.py
- folder
- sub2.py
class Apple:
def __init__(self, value):
self.value = value
def mean(a, b):
return (a+b)/2
from sub1 import mean
from folder.sub2 import Apple
apple1 = Apple(value=100)
apple2 = Apple(value=200)
result = mean(apple1.value, apple2.value)
print(result)
$ stickytape main.py > onefile.py
#!/usr/bin/env python
import contextlib as __stickytape_contextlib
@__stickytape_contextlib.contextmanager
def __stickytape_temporary_dir():
import tempfile
import shutil
dir_path = tempfile.mkdtemp()
try:
yield dir_path
finally:
shutil.rmtree(dir_path)
with __stickytape_temporary_dir() as __stickytape_working_dir:
def __stickytape_write_module(path, contents):
import os, os.path
def make_package(path):
parts = path.split("/")
partial_path = __stickytape_working_dir
for part in parts:
partial_path = os.path.join(partial_path, part)
if not os.path.exists(partial_path):
os.mkdir(partial_path)
open(os.path.join(partial_path, "__init__.py"), "w").write("\n")
make_package(os.path.dirname(path))
full_path = os.path.join(__stickytape_working_dir, path)
with open(full_path, "w") as module_file:
module_file.write(contents)
import sys as __stickytape_sys
__stickytape_sys.path.insert(0, __stickytape_working_dir)
__stickytape_write_module('sub1.py', 'def mean(a, b):\n return (a+b)/2')
__stickytape_write_module('folder/sub2.py', 'class Apple:\n\n def __init__(self, value):\n self.value = value')
from sub1 import mean
from folder.sub2 import Apple
apple1 = Apple(value=100)
apple2 = Apple(value=200)
result = mean(apple1.value, apple2.value)
print(result)
150.0
방금 코드를 Google Colab에 복사하여 실행했습니다.
아래와 같이 무사히 150.0이 표시되었습니다.
(무지개 빛깔의 고양이가 걷는 것을 걱정하지 마십시오.)
스크립팅
여기에서 여담입니다.
stickytape
라는 명령은 길고 일단 생성 파일의 디렉토리를 지정하거나 게을리하지 않으므로 다음과 같이 스크립팅하는 것이 좋습니다.
- main.py
- sub1.py
- folder
- sub2.py
- scripts
- tape.sh
- build
- onefile.py
tape.sh# 初期値
entry="main.py"
output="onefile.py"
# オプション
while getopts e:o: OPT
do
case $OPT in
"e" ) entry=${OPTARG};;
"o" ) output=${OPTARG};;
esac
done
# 実行
stickytape ${entry} > "build/${output}"
다음 명령은 main.py를 실행하여 빌드 디렉토리에 onefile.py를 생성합니다.
$ sh scripts/tape.sh
옵션도 준비했습니다.
옵션 이름
설명
-e
진입점 파일 이름
-o
출력할 파일 이름
$ sh scripts/tape.sh -e <ファイル名> -o <ファイル名>
생성 된 디렉토리는 build
로 고정되어 있으므로 싫다면 마음대로 변경하십시오.
자기소개
서두에 쓰면 방해가 되므로 마지막에 몰래 자기 소개시켜 주세요.
이름
코토오카 아키키
학교
고베 대학 대학원
학부 연구
기계 학습, 음성 처리
대학원 연구
기계 학습, 공정성, 생성 모델, etc
트위터
@Wataoka_Koki
트위터를 따르십시오!
Reference
이 문제에 관하여(여러 파이썬 파일을 하나의 파이썬 파일로 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/wataoka/items/dfc67048c7f214a31265
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
- main.py
- sub1.py
- folder
- sub2.py
- scripts
- tape.sh
- build
- onefile.py
# 初期値
entry="main.py"
output="onefile.py"
# オプション
while getopts e:o: OPT
do
case $OPT in
"e" ) entry=${OPTARG};;
"o" ) output=${OPTARG};;
esac
done
# 実行
stickytape ${entry} > "build/${output}"
$ sh scripts/tape.sh
$ sh scripts/tape.sh -e <ファイル名> -o <ファイル名>
서두에 쓰면 방해가 되므로 마지막에 몰래 자기 소개시켜 주세요.
이름
코토오카 아키키
학교
고베 대학 대학원
학부 연구
기계 학습, 음성 처리
대학원 연구
기계 학습, 공정성, 생성 모델, etc
트위터
@Wataoka_Koki
트위터를 따르십시오!
Reference
이 문제에 관하여(여러 파이썬 파일을 하나의 파이썬 파일로 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/wataoka/items/dfc67048c7f214a31265텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)