2016-06-27 7 views
1

.py 파일 확장자없이 가져 오기를 시도했습니다. 예 : import something. 그것은 작동하지 않습니다. 또한 .py로 초기화하여 패키지로 만들려고했으나 여전히 가져오고 싶지 않습니다. 오 그런데 파이썬 3.5를 사용하고 있습니다. 편집 : 나는 Pythonista를 사용하고 있으며 현재 컴퓨터 및/또는 Xcode에 액세스 할 수 없습니다.iOS에서 python 파일을 가져올 수 없습니다. pythonista

답변

1

pythonista에서 Documents 폴더는 기본적으로 sys.path에 없습니다. site-packages 폴더 또는 실행중인 현재 스크립트의 경로입니다. 콘솔에서 import foo을 시도하면 foo.py가 포함 된 경로가 sys.path에 있지 않으면 실패합니다.

세 가지 기본 옵션이 있습니다 특히 추가) 1) 모듈 & 템플릿에서 발견 (사이트 패키지로 "라이브러리"를 넣어,하지만 정말 경로 ~/문서/사이트 패키지)

2 가져 오기 전에 경로 (경로는 sys.path에있는 경우 중복하지 않도록 먼저 확인해야하지만) 그런 당신이 foo.py 내부 문서를 가지고있는 것처럼/somefolder

sys.path.append(os.expanduser('~/Documents/somefolder')) 
import foo 

3) 0 사용

0

iOS에서이 작업을 수행 할 수 있는지 알지 못했지만 다른 사용자의 수정 사항을 발견했습니다. 이 코드는 제 것이 아니라 sorin입니다.

import os, sys, inspect 
# realpath() will make your script run, even if you symlink it :) 
cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe()))[0])) 
if cmd_folder not in sys.path: 
    sys.path.insert(0, cmd_folder) 

# use this if you want to include modules from a subfolder 
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0],"subfolder"))) 
if cmd_subfolder not in sys.path: 
    sys.path.insert(0, cmd_subfolder) 

# Info: 
# cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!! 
# __file__ fails if script is called in different ways on Windows 
# __file__ fails if someone does os.chdir() before 
# sys.argv[0] also fails because it doesn't not always contains the path 
관련 문제