I have a bunch of useful scripts that I want to import from time to time. How to best organize them? I would want them to be on my /home/ folder -- is that possible? Is that the best way?
On a related note, when my other scripts import my local scripts, is there a best practice to make them portable? Should I include notes in my script to alert readers / myself that I'm importing from self-written script?
Thank you!
In your .bashrc you can specify the $PYTHONSTARTUP and $PYTHONPATH parameters. I have the following in my own .bashrc:
export PYTHONSTARTUP=$HOME/.config/python/pythonrc.py
export PYTHONPATH=$PYTHONPATH:$HOME/.config/python/path
Note that The .bashrc file is for bash specifically. Other shells may have other files loaded at startup.
The $PYTHONSTARTUP script is run every time you start a python console. This is useful if you want to add tab completion for example. For example in the case I specified, whenever you run python from the terminal, the script .config/python/pythonrc.py is executed before the console starts.
You can put python packages which should be importable anywhere in the $PYTHONPATH you specified. So basically $PYTHONPATH for python has some similarities to $PATH for bash. Note this is not $PATH. I do not recommend messing with the $PYTHONPATH though. I think it is better to append the paths to sys.path in the $PYTHONSTARTUP script.
And then there is the usercustomize module. If there is a module named usercustomize anywhere in the path, it will be imported by all python processes. For usercustomize to work you do need to make sure that this is in your $PYTHONPATH. For usercustomize you do need to set it in $PYTHONPATH, but you could append more paths in usercustomize.py just like in $PYTHONSTARTUP, so you only need to add 1 more directory to the $PYTHONPATH.
How to add a folder to your path for Mac
You may want to organize your custom scripts into a folder that is not mixed in with the Anaconda modules
1) Find your bash profile. In Terminal on the command line type (without the $ sign, which is the prompt)
$ cd
$ ls -a
This means "change directory to my home folder, list all files including hidden files". You should see a file called .bash_profile listed there
2) Edit your bash profile.Into the command line type
$ open .bash_profile
This should open in TextEdit. Add two lines to the text file:
# Added by My Name on My Date
export PYTHONPATH="/Users/myusername/path/to/folder:$PYTHONPATH"
3) Check that this worked. Open a python session:
>>> import sys
>>> sys.path
The new folder should now be listed in your path
Related
I need to run python scripts from anywhere on my Linux pc.
I have a folder name Tools that hold 100 Scripts and I wonder if there is any way to just write the name of the script in the terminal without having to move to the specific folder.
You could add that folder to your $PATH variable:
export PATH=${PATH}:/Tools
If you place this statement in a file that's executed when you open a shell (e.g., .bashrc if you use bash), you won't need to manually run it again.
Try to create alias: $ alias shortName="your custom command here"
https://www.tecmint.com/create-alias-in-linux/
I have been looking into bash and shell recently and been trying to work out how git is able to make a terminal command that runs C scripts in the current directory e.g git init, git push/pull etc.
I have been trying to simulate it in python, by making an executable python script in my home directory,
#!/usr/bin/env python
import os
print("current_dir: ",os.getcwd()) #prints /Users/usr/folder/script.py
Then I create a .command file that calls the python script
cd
cd FOLDER/
python3 SCRIPT.py
and editing the bash profile to export a variable to run the .command file.
export mycommand=/Users/urn/folder/command.command
Although this is not even nearly close to the way git achieves its command line. For example, when I run my script is not actually a terminal command it is just an environment variable, hence the $.
$mycommand
Secondly, this goes to the directory of the python file and runs the script from with that directory, therefore the output will always be the same
/Users/usr/folder/script.py
Although in git it runs the file in the current directory. Therefore the print statement would change depending on the terminal directory.
How am I able to create my own 'terminal command' such as git init to run my python script in whatever directory I'm in. Ps, I'm on mac.
Any help would be greatly appreciated :)
It sounds like you are missing at least two basic concepts:
The search path: when you issue a command that is not a shell function or built-in and that has an unqualified name (one with no / characters), the shell searches a list of zero or more directories for a matching executable file. This list of directories is the "path", and it is stored in the PATH environment variable. You can change it if you wish. This is how the shell finds the git program when you do not specify a path to it.
Executable scripts: Python, shell, Perl, etc. programs that must be run via an interpreter can be made executable by name alone by including an appropriate shebang line as the very first line and assigning an executable mode. You include an appropriate shebang line in your example Python program, in fact, but you seem not to understand its significance, because you explicitly launch the script via the python3 command. The shebang line is just another comment to Python, but it is meaningful to the system.
It seems like you probably also are missing some other concepts, like the fact that your script doesn't need to be in the current working directory for you to run it via the python3 launcher, path notwithstanding. Just specify its full pathname. Alternatively, Python has its own variation on a path, PYTHONPATH, by which it can locate modules and packages. These are alternatives, however -- the first two points are enough to achieve your apparent objective. Specifically,
Keep the shebang line in your script, though your default python is probably v2.7, so if you really want to run it specifically via python3 then modify the shebang line to say so:
#!/usr/bin/env python3
Make sure the file has executable mode. For example,
chmod 0755 /Users/urn/bin/SCRIPT.py
Then you should be able to execute the script from anywhere via its full pathname.
To access it from anywhere via its simple name, ensure that the directory containing it is in your path. For this purpose, it would be wise to choose an appropriate directory, such as /usr/local/bin or /Users/urn/bin (you may need to create the directory first). Whichever you choose, ensure that that directory is in your PATH. For example, edit /Users/urn/.bash_profile, creating it if necessary, and ensure that it contains (say) the commands
PATH=$PATH:/Users/urn/bin
export PATH
That will take effect in new Terminal windows you open afterward, but not automatically in any that are already open. In those windows, you will be able to run the script, from anywhere, via its simple name.
On Windows 10, Python 3.6.1, after I added E:\learning python exercise in PYTHONPATH by the GUI or through .pth file. Ihen C checked the os.sys.path, it reads:
But when I want to run a file through the cmd prompt, it pops out error:
Python does not use PYTHONPATH for scripts - it uses them for modules. So you could try:
python -m script1
but that is not the way scripts should be run. You should supply the path name to python or assume it is in the current directory. For example:
python "E:\learning python exercise\script1.py"
The double quotes are required because you have elected to have spaces in your directory name - I suggest that you might want to stop doing that, spaces in path names just makes life more difficult.
Alternatively, change your current directory, either using the cd command or by setting the "start in" properties to your cmd.exe shortcut. For example:
cd "E:\learning python exercise"
python script1.py
You only have to do the cd once in each cmd.exe session.
If you have setup the filename association for python (should have been done at installation time), then you should only need:
cd "E:\learning python exercise"
script1.py
I'm finding that python is modifying my path in problematic ways -- in particular, it's pre-pending the path to my github development folder, which results in the wrong libraries being loaded.
In my terminal session, if I run echo $PATH I get:
~$echo $PATH
/Users/Nick/anaconda/bin:/usr/local/bin:/usr/bin:
/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin
Which is perfect. But when I launch python from that same terminal session (either as python or ipython) and check my PATH from within python, I'm getting:
>>> print(sys.path)
['', '/Users/Nick/anaconda/lib/python3.4/site-packages/Cython-0.22.1-py3.4-
macosx-10.5-x86_64.egg', '/Users/Nick/github/pandas',
'/Users/Nick/anaconda/lib/python34.zip', '/Users/Nick/anaconda/lib/python3.4',
'/Users/Nick/anaconda/lib/python3.4/plat-darwin',
'/Users/Nick/anaconda/lib/python3.4/lib-dynload',
'/Users/Nick/anaconda/lib/python3.4/site-packages',
'/Users/Nick/anaconda/lib/python3.4/site-packages/Sphinx-1.3.1-py3.4.egg',
'/Users/Nick/anaconda/lib/python3.4/site-packages/aeosa',
'/Users/Nick/anaconda/lib/python3.4/site-packages/setuptools-18.0.1-py3.4.egg']
Where are these coming from and how do I stop them?
Thanks!
PATH has nothing to do with the Python module search path; that environment variable is used by your shell to find executables, instead.
You need to look at the PYTHONPATH variable here.
If that variable doesn't contain your extra elements, start Python with the -S command line switch to disable loading site.py; it may be that the extra elements are set by a .pth file. Also see the site module documentation:
A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path.
While $PATH seems like it may be used by Python, what you actually want to look at is your $PYTHONPATH -- which is used by the import machinery and logic.
You should look into using virtualenvironments to control the pathing of Python module lookups.
I am a Python beginner and I am having trouble running Python from CMD. I have added the Python installation directory as a PATH variable (;C:\Python27). I am able to run the Python Interpreter from CMD, however when I issue a command like "python file.py command" from CMD, it returns "Error2, Python can't open, no such file/directory".
So what I do is go to "cd C:\Folder\Folder2\My_Python_Files", then type the "file.py command" each and every time. Is there faster or more efficient way of doing this? I am currently running Python2.7 on Windows 8.
When you run python <script>, it requires an actual path to the script being provided. You cannot specify "file.py" alone, unless it is right there in your current directory.
In windows, here are two steps you can take:
Associate .py files with python. Then you can run them directly without the python command as: /path/to/file.py
(right click a .py -> properties -> change to associate with python.exe)
Further step: Add a location to your PATH environment which will contain your python scripts. From there, you can just do file.py and it will be found in your search path.
So you could add C:\Folder\Folder2\My_Python_Files to your PATH and that is where you can store your executable python scripts.
Also you can set the PATH variable temporarily during a shell session:
SET PATH=%PATH%;C:\path\to\project
Just like PATH environment variable lists several directories for the system to search for executables, the PYTHONPATH do the same for Python to search for .py files. If you want scripts in a folder to be globally accessible (i.e. you can reference them by name just like you want, or you can import them from other scripts), add that folder to PYTHONPATH (create it if it doesn't exist).
Note that the command to invoke a script that is in your PYTHONPATH is:
python -m file [<script arguments>]
(i.e. use the -m option to treat it as a module, and don't use the extension .py)
Here's an article explaining in more detail how Python finds its source files (both in the command line and through import).
Note that you can also refer to the script by using its full path:
python C:\Folder\Folder2\My_Python_Files\file.py command
But by doing this, other files in the same folder that this script might reference through import might not work (since Python doesn't know where to search for them).
Unless the project's folder is in the PATH, you cannot call the file unless you are inside the project's folder. Don't create PATHs for projects, unless they are needed; it's unnecessary.
Just transverse to the file's directory and run the command inside the directory. That will work.
If the project will be used by other projects/files, you can use PYTHONPATH to set the directory, so the other projects can successfully access it.
Hope that helps.