私はJupyter NotebookのPython 3.4.2と でサーバを使用しています。ノートブックは他のユーザからも1台でも複製されるため、グローバル名__file__
を使用します。 path
paramは、相対または絶対パスであり、path
の絶対的なバージョンを返す場合__file__はJupyter Notebookには存在しません
def __init__(self, trainingSamplesFolder='samples', maskFolder='masks'):
self.trainingSamplesFolder = self.__getAbsPath(trainingSamplesFolder)
self.maskFolder = self.__getAbsPath(maskFolder)
def __getAbsPath(self, path):
if os.path.isabs(path):
return path
else:
return os.path.join(os.path.dirname(__file__), path)
__getAbsPath(self, path)
チェック:セクションIは、実行しなければなりません。だから私は返されたpath
を後で安全に使うことができます。
しかし、私は
/usr/local/lib/python3.4/dist-packages/ipykernel/__main__.py
しかし、正しいノートの場所オンラインこのエラーを検索し、私はより良いsys.argv[0]
を使用する必要があることを「解決策」を見つけましたが、print(sys.argv[0])
戻り
NameError: name
'__file__'
is not defined
を取得/home/ubuntu/notebooks/
である必要があります。マルタインピータース(コメント)から参照How do I get the current IPython Notebook nameため
おかげで、最後の答えは(受け入れられない)私のニーズにぴったりのフィット:
print(os.getcwd())
/home/ubuntu/notebooks
'__file__' *は、ないノートPCに*モジュールやPythonスクリプトに適用されます。あなたが見つけた答えはここでは適用されません。 –
[現在のIPythonノートブックの名前を取得する方法](http://stackoverflow.com/q/12544056)より関連性がありますか? –