This is going to be a multi-part question but the ultimate aim is such that I can access custom-made modules/libraries/functions like how I do in native python.
Where are the non-native but pip
installed python libraries stored and how to configure my interpreter/IDE to access them?
My users' script all starts with:
#!/usr/bin/env python -*- coding: utf-8 -*-
What's the difference between accessing from /usr/bin
and /usr/bin/env
, will the custom-made modules that should import like native python modules/packages work?
Should my custom scripts become packages? if so how do I make the user-side code, checks for ImportError
and install/setup these packages in the try-except
? e.g.
try:
import module_x
except ImportError:
# Install package, but how to do it within the script?
pass
Is there a place to store my custom scripts such that it imports like a native library? If so, where? What are the consequences?
Well, you've asked a lot of questions; I will address the one in the subject line.
You can put Python module files anywhere you want and still import
them without any problems as long as they are in your module search path. You can influence your module search path by altering the environment variable PYTHONPATH
in your shell before invoking Python, or by altering the sys.path variable inside your code.
So if you've installed /home/alvas/python/lib/module_x.py
and /usr/local/python/lib/foo.py
you could run:
PYTHONPATH=/home/alvas/python/lib:/usr/local/python/lib /home/alvas/scripts/bar.py
and then the statements
import module_x
import foo
should simply work.
Alternatively you could do something like this in your code:
#!/usr/bin/env python
import sys
sys.path.append('/home/alvas/python/lib')
import module_x
sys.path.append('/usr/local/python/lib')
import foo
Either approach will work.