Python - add PYTHONPATH during command line module run
For Mac/Linux;
PYTHONPATH=/foo/bar/baz python somescript.py somecommand
For Windows, setup a wrapper pythonpath.bat
;
@ECHO OFF
setlocal
set PYTHONPATH=%1
python %2 %3
endlocal
and call pythonpath.bat
script file like;
pythonpath.bat /foo/bar/baz somescript.py somecommand
How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
You know what has worked for me really well on windows.
My Computer > Properties > Advanced System Settings > Environment Variables >
Just add the path as C:\Python27 (or wherever you installed python)
OR
Then under system variables I create a new Variable called PythonPath
. In this variable I have C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk;C:\other-folders-on-the-path
This is the best way that has worked for me which I hadn't found in any of the docs offered.
EDIT: For those who are not able to get it,
Please add
C:\Python27;
along with it. Else it will never work.
Python: Add to Include Path on the Command Line
Use the PYTHONPATH
environment variable:
PYTHONPATH=lib/ python test/my-test.py
Permanently add a directory to PYTHONPATH?
You need to add your new directory to the environment variable PYTHONPATH
, separated by a colon from previous contents thereof. In any form of Unix, you can do that in a startup script appropriate to whatever shell you're using (.profile
or whatever, depending on your favorite shell) with a command which, again, depends on the shell in question; in Windows, you can do it through the system GUI for the purpose.
superuser.com
may be a better place to ask further, i.e. for more details if you need specifics about how to enrich an environment variable in your chosen platform and shell, since it's not really a programming question per se.
How can I use a Python script in the command line without cd-ing to its directory? Is it the PYTHONPATH?
I think you're a little confused. PYTHONPATH sets the search path for importing python modules, not for executing them like you're trying.
PYTHONPATH Augment the default search path for module files. The
format is the same as the shell’s PATH: one or more directory
pathnames separated by os.pathsep (e.g. colons on Unix or semicolons
on Windows). Non-existent directories are silently ignored.In addition to normal directories, individual PYTHONPATH entries may
refer to zipfiles containing pure Python modules (in either source or
compiled form). Extension modules cannot be imported from zipfiles.The default search path is installation dependent, but generally
begins with prefix/lib/pythonversion (see PYTHONHOME above). It is
always appended to PYTHONPATH.An additional directory will be inserted in the search path in front
of PYTHONPATH as described above under Interface options. The search
path can be manipulated from within a Python program as the variable
sys.path.
http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
What you're looking for is PATH.
export PATH=$PATH:/home/randy/lib/python
However, to run your python script as a program, you also need to set a shebang for Python in the first line. Something like this should work:
#!/usr/bin/env python
And give execution privileges to it:
chmod +x /home/randy/lib/python/gbmx.py
Then you should be able to simply run gmbx.py
from anywhere.
Provide temporary PYTHONPATH on the commandline?
You can specify the python path in an environment variable like so:
PYTHONPATH=/path/to/some/necessary/modules python3 my_script.py
Related Topics
Pandas Group by and Find First Non Null Value for All Columns
How to Use Virtualenv with Python
Python List Directory, Subdirectory, and Files
How to Use Opencv's Connectedcomponentswithstats in Python
Importerror: No Module Named Crypto.Cipher
Add 'Decimal-Mark' Thousands Separators to a Number
Principal Component Analysis (Pca) in Python
Installing Numpy on 64Bit Windows 7 with Python 2.7.3
Scrape Website with Dynamic Mouseover Event
How to Hide the Console Window in a Pyqt App Running on Windows
Accessing Attributes on Literals Work on All Types, But Not 'Int'; Why
Retrieving a Foreign Key Value with Django-Rest-Framework Serializers