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.
Running python from command line, only project imports are failing
You should had your package to the python path
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
What's the point of adding a program to PATH?
Very short answer, somebody can provide details also I am not an expert.
If in windows, adding to path is like adding the program to the environment variables. This means, that instead of executing it to the full path where the .exe is you could call it with an "alias".
To run python, instead of going somewhere like C:/Program Files/Python/python.exe you could simply type "python".
What is the Python equivalent of Matlab's pathtool adding?
To provide complete steps for anyone with my problem for later times, I found my file with functions can be thought of as a module from which functions can be imported just like from file_with_functions import my_function
.
But, I needed to add the folder with my module to python path. Messing with environment variables in Windows didn't work for me. Fortunately here, I read about a different solution:
I simply went to my site-packages folder (to find it, import sys
then print(sys.path)
and look for a name containing the string 'site-packages'). In this folder, I created a new text file and simply pasted the path with my module there:
like this, closed the text file and changed the extension from .txt to .pth (name of file did not matter as long as it was a .pth, Python found it).
Running Python scripts through the Windows Command Line
From the book (p. 44, 4th Ed):
Finally, remember to give the full path to your script if it lives in a different directory from the one in which you are working.
For your situation, this means using
C:\User\Example> python C:\User\Example\my_scripts\script1.py
You could write a batch file that looks for the script in a predefined directory:
@echo off
setlocal
PATH=C:\User\Example\Python36;%PATH%
SCRIPT_DIR=C:\User\Example\my_scripts
python %SCRIPT_DIR\%*
How can i know the name of a imported module? PYTHON
For installing from zip simply use:
pip install *.zip
or specify the path directly:
pip install <path to .zip>
pip install ./my-archive.zip
Same applies for a tarball or any other format. It can be even a folder. However, it has to include a proper setup.py
or other mechanism for pip to install it and pip has to support the packaging format (be it archive, networking protocol, version control system (git
prefix), etc).
pip install ./my-folder
pip install ./
pip install .
pip install ..
etc
If, however, there is no setup.py
present, you'll need to simply copy-paste the files somewhere where your project/module resides (or set PYTHONPATH
or sys.path
to that folder) to be able to import them. See this or this question for more.
Related Topics
Splitting a List into N Parts of Approximately Equal Length
Activate a Virtualenv with a Python Script
Unicodedecodeerror: 'Utf8' Codec Can't Decode Byte 0Xa5 in Position 0: Invalid Start Byte
Secondary Axis with Twinx(): How to Add to Legend
Convert Django Model Object to Dict with All of the Fields Intact
Index a 2D Numpy Array with 2 Lists of Indices
Skip the Headers When Editing a CSV File Using Python
Getting List of Parameter Names Inside Python Function
How to Check for Valid Email Address
How to Convert Each Item in the List to String, for the Purpose of Joining Them
Keep Only Date Part When Using Pandas.To_Datetime
Getting the Index of the Returned Max or Min Item Using Max()/Min() on a List