How to Use a Python Script in the Command Line Without Cd-Ing to Its Directory? Is It the Pythonpath

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



Leave a reply



Submit