0

Can anyone let me know how to set PYTHONPATH?

Do we need to set it in the environment variables (is it system specific) or we can independently set the PYTHONPATH and use it to run any independent python application?

i need to pick the module from a package available in directory which is different from the directory from which I am running my application . How to include these packages in my application

5
  • It is system specific, You may be confused with the imporations of the module Commented Jul 17, 2017 at 12:41
  • 2
    What do you mean by "independent python application"? Please describe what the problem is. Commented Jul 17, 2017 at 12:46
  • This answer depends on the OS, so how would it be "independent"? Commented Jul 17, 2017 at 13:09
  • i need to pick the module from a package available in directory which is different from the directory from which I am running my application . How to include these packages in my application Commented Jul 17, 2017 at 13:13
  • Possible duplicate of Importing files from different folder Commented Jul 17, 2017 at 13:35

2 Answers 2

1

I assume you are using Linux

Before executing your application u can metion pythonpath=path && execution script

Other elegant way is using virtualenv. Where u can have diff packages for each application. Before exection say workon env and then deactivate

Python3 has virtualenv by default

Sign up to request clarification or add additional context in comments.

2 Comments

venv is included, not virtualenv. Also && isn't necessary
&& is for santiy. venv is similar to virtualenv
0

Instead of messing with system environment variables and paths set therein, if you only need to add a specific directory to the module lookup path of your application use:

import sys

sys.path.append("path/to/your/packages")

It's not recommended to mess with your paths in general - if you have to, you should probably rethink your application design - but if you still decide that you have to, this is more preferable than messing with system paths.

Comments

Your Answer

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.