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

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • It is system specific, You may be confused with the [imporations of the module](https://stackoverflow.com/questions/19917492/how-to-use-pythonpath) – m.nachury Jul 17 '17 at 12:41
  • 2
    What do you mean by "independent python application"? Please describe what the problem is. – mzjn Jul 17 '17 at 12:46
  • This answer depends on the OS, so how would it be "independent"? – OneCricketeer Jul 17 '17 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 – Aakash Agrawal Jul 17 '17 at 13:13
  • Possible duplicate of [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – OneCricketeer Jul 17 '17 at 13:35

2 Answers2

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

Jileshl
  • 146
  • 6
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.

zwer
  • 24,943
  • 3
  • 48
  • 66