2

Is it possible to specify directories to search for python imports in the python startup command?

Environment variables are great but the loosely coupled dependency between a binary and required environment variables isn't ideal for my case (a 1-liner is better than a 2 liner for extra foolproofness; I know you can do the 1.5 liner where you put the shell variable assignment as a prefix to the command you're about to execute).

asked Nov 16, 2013 at 2:18

1 Answer 1

3

PYTHONPATH environmental variable is used to set sys.path. It is a simple list of strings so you can always use sys.path.append inside your module:

import sys
sys.path.append('/some/very/useful/path')

Note:

It can be particularly useful when combined with os.path.abspath

 import os
 sys.path.append(os.path.join(
 os.path.abspath(os.path.dirname(__file__)), 'another/useful/directory'
 ))
answered Nov 16, 2013 at 2:28
Sign up to request clarification or add additional context in comments.

1 Comment

That's an acceptable solution for my hello world samples. Thanks.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.