I am new to python and OSX terminal commands. I wanted to permanently add a directory to PYTHONPATH, thus I looked here and used these instructions:
1-Open up Terminal
2-Type open .bash_profile
3-In the text file that pops up, add this line at the end: export PYTHONPATH=$PYTHONPATH:foo/bar
4-Save the file, restart the Terminal, and you're done
I also followed this instruction:
export PYTHONPATH="${PYTHONPATH}:/my/other/path"
After these changes I cannot use any command like python, which, whereis, pip, etc. in Terminal :(
The response is something like below:
-bash: which: command not found
I do not know what I have done.
are you sure you have appended the path because what i can see is you have replaced old path definition and assigned a new one
remove bash profile use one of these commands /bin/rm ~/.bash_profile Or if you want to save your current bash configs then use a mv instead /bin/mv ~/.bash_profile ~/bash_profile Then close your terminal and start again you will be able to use all commands and apps
Related
Yes, I know I can do
python2 cal.py
What I am asking for is a way to execute it on the command line such as:
calpy
and then the command afterwards. I put in in a path and when I write cal.py in the command line:
/usr/bin/cal.py: line 5: print: command not found
I don't want to issue cal.py to run my script, I want it to be issued with calpy
I'm running Arch Linux if that helps, thanks. Sorry for my English.
In order for bash to know to run your script via the Python interpreter, you need to put an appropriate shebang at the start. For example:
#!/usr/bin/python
tells bash to run /usr/bin/python with your script as the first argument. I personally prefer
#!/usr/bin/env python
which is compatible with virtualenv. You also need to ensure that the permissions on your script allow it to be executed:
~$ chmod +x path/to/cal.py
Finally, in order to call cal rather than path/to/cal.py, you need to remove the .py extension and make sure that the directory containing cal is in your command search path. I prefer to add ~/bin to the search path by modifying the $PATH environment variable in ~/.bashrc:
export PATH=$HOME/bin:$PATH
then put my own executables in ~/bin. You could also copy (or symlink) cal to one of the system-wide binary directories (/bin or /usr/bin), but I consider it bad practice to mess with system-wide directories unnecessarily.
Ok, you need a couple of things for achive what you want.
First you have to tell your script "How" is going to execute/interpret it. You can do this writting
#/usr/bin/env python
at the very beggining of the file.
The problem you have is the system is trying to execute the script using bash. And in bash there is no print command.
Second you need give execution privileges to your script. And of course if you want to call your script through the command "calcpy", the script has to be called like that.
Put this (exactly this) as the first line of your script:
#!/usr/bin/env python
So I uninstalled Enthought Canopy (I think completely, but I'm unsure if there's residual information hanging around that I neglected to modify).
After uninstalling, following the directions here: https://support.enthought.com/entries/23580651-Uninstalling-and-resetting-Canopy
I was slightly unsettled by the fact that I couldn't find anything to delete for step 5, and since Canopy was never my default Python environment, I didn't think too much of it.
But now, every time I open terminal, I get this warning:
"-bash: /Users/MyName/Library/Enthought/Canopy_64bit/User/bin/activate: No such file or directory"
What do I need to do to fix this? I'm running Mac OSX, 10.8.5 (Mountain Lion). Any help would be much appreciated.
Until today I had this exact same problem, and it really bothered me. I solved it this way:
First, I entered following command in the terminal: open -a TextEdit .bash_profile
This opens your .bash_profile in TextEdit, which for a terminal novice like me at least, is easier than editing it in the terminal with vi or something like that. In that file, you should see a few lines related to "Canopy" and "Enthought". In my case, this is what showed up:
# Added by Canopy installer on 2014-02-11
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make bashprompt show that Canopy is active, otherwise 1
VIRTUAL_ENV_DISABLE_PROMPT=1 source /Users/MyUserame/Library/Enthought/Canopy_64bit/User/bin/activate
I think it is that last line that is causing all the trouble. Simply delete these few lines (don't delete anything else). In my case, these lines were in a separate block from others in my .bash_profile so it was easy to pick them out. After this, just close the text file, restart your computer, and the annoying stuff about Enthought not being found should be gone. For me this worked, hope it does for you too!
To find out where this is, run an interactive shell with the xtracefd flag set and a PS4 that includes $BASH_SOURCE. For instance:
PS4='+$BASH_SOURCE:$LINENO:' bash -i -x
Then, look through the output for the first reference to the activate script given; it will include the source file and line number.
Most likely, this will be .bashrc.
For anyone who is having this problem as I did, I found the source in my .profile file. You will see a command at the top of your .bash_profile or .bashrc that reads: [[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile. This is calling yet another script called .profile that you need to clean up. To edit .profile use the command:
open -a TextEdit .profile
Then you'll find a line in there that says something like:
# Added by Canopy installer on 2013-04-10
source /Users/username/Library/Enthought/Canopy_64bit/User/bin/activate
Delete those lines and save, et voila!
I had a similar error message. You need to comment out (#) or delete the last line thats under the #Canopy settings in your bash profile. follow below steps in terminal to get rid of the error message:
vim .profile
You should see the following info regarding your Canopy set up:
# Added by Canopy installer on 2016-01-12
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make bashprompt show that Canopy is active, otherwise 1
VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/waismoradi/Documents/CanopyEnthought/User/bin/activate'
Just delete or comment out with -> # the last line and you are all set! This will disable the env prompt to show up when you launch terminal.
I have created this python script called 'SleepCalc.py'. It is a simple script that tells me when to wake up based on 90 minute sleep cycles. Usually I open terminal, cd command my way to the dir containing the .py and then execute the script with the 'python' command.
What I want to know is how can I make an app/automator workflow/apple script that I can double click and it executes the python script in the terminal without me having to cd, etc.
http://codebin.org/view/98c0b7c5
Add shebang: #!/usr/bin/env python at the top of your script.
And then give the file permission to execute by:
chmod +x SleepCalc.py
You open Terminal and enter nano at the top. It opens up a Terminal-based text editor.
After that, type python followed by the file path (at the beginning, include ~/ because the computer starts at the root).
Do Control-X, click Yes, and save it as launch.command.
After that, type chmod +x and drag the newly created file into the Terminal window. This gives permission for the file to execute.
From then on, you can double click the .command file to launch SleepCalc.py.
This is a follow up question as I'm trying to move forward with Zelle's Python:Programming.
It appears that IDLE 2.7.2 has hang issues when opening graphics windows in interactive mode. But, I can simply run Python interactively in Terminal and don't have any of those issues. So that's a big help. Zelle provides a simplified graphics file called graphics.py, to get up to speed with objects and graphics, before dealing directly with Tkinter.
My question is this. Where should I put graphics.py, so Terminal will see it (when it's called), without including the full path every time? Thanks,
Henry
According to PEP 0370, a good place to put your user packages is in ~/.local/lib/python2.7/site-packages.
Modules in here should be importable.
You can modify your ~/.bashrc to include a PYTHONPATH definition that includes the location of the file.
export PYTHONPATH="/path/to/directory_containing_graphics.py"
Just be careful if it's in your home director to use /Users/[username]/etc/directory_containing_graphics.py, I vaguely recall being bitten by ~ not expanding like I expected one time on OS X.
I went deep down the rabbit hole to get to bottom of this issue. It turns out that bash on OSX has a few quirks, not unexpected with the various flavors of UNIX around. It turns out that when firing up a bash shell, bash looks in .bash_profile for config statements. If you enter bash into bash, you fire up a sub shell, and that's when and only when .bashrc is executed.
To make sure you only have to manage one config file, put:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
in your .bash_profile. This just tells bash to look into the .bashrc file if it exists. You can then leave that file alone and only modify your .bashrc file and all instances of bash will be modified the same. Once I figured this out, I still had to solve my problem. Here's the best I could do. When Python installed it installed this into my .bash_profile:
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
This tells bash where to look for the libraries needed to run Python. So I moved this statement to my .bashrc file after putting the "if" statement into my .bash_profile. I then tried adding a : to the PATH statement with the path to my Python modules, but that didn't work. So, I have settled for a statement like this in .bashrc:
PYTHONPATH=$PYTHONPATH:/Users/ftpbub/Documents/workspace/Python\ Programming\ Modules/src/
export PYTHONPATH
Now I enter python at the command prompt, and when I get to python, I can enter import modulename without the path and it works. If I start a new project in a new directory, I should be able to add it to my PYTHONPATH and be good. I would have liked to be able to enter python modulename right into bash and have the module execute, but I haven't figured out how to do that. Other than that, I think this the best I can do. Ideas for how to go straight to the module from the bash prompt, without entering the path?
I've tried many variations of this command: idle.py -e filepath, but it simply starts IDLE like normal, not opening any extra windows for editing, and not throwing any errors.
So how can I do the equivalent of opening IDLE, file>open>filepath via the command line (or perhaps even a Python module)?
You need to do as stated in the main.py file of the idelib folder (C:\Python33\Lib\idlelib), at least on the python 3.3 version explains that:
IDLE main entry point
Run IDLE as python -m idlelib
So with python -m idlelib <script_to_edit> you will be able to open and edit the script with idle. I haven't checked with previous versions but it could be the same comand
This is also documented on the changelog of the version 3.3.3
Make a new text file, and put something like this in it:
C:\Python26\Lib\idlelib\idle.pyw "C:\file1.py" "C:\file2.py"
In your actual script, you'll replace "C:\file1.py" and "C:\file2.py" with your files' paths, save as a .bat, and then launch it. That should do what you want.
Please forgive me for bumping such an old thread, but I've been teaching myself linux and python with the help of the community, and was trying to figure out how to invoke IDLE2 and IDLE3 from the command line. I came across this post some of the solutions seemed a bit complicated for my application. Then it occurred to me that I could just put syslinks in the /usr/bin/ path for each.
sudo ln -s idle-python3.1 idle3
sudo ln -s idle-python2.6 idle2
to address the OP. From the directory the script is located, type:
idle3 abc123.py
or
idle2 abc123.py
I'm just so damned happy that I finally had a "light bulb" go off that I wasn't going to let a 2 year old post stop me from posting my solution.
Rarely the native os is useful. I created a 'win batch file, in the folder with my .py files:
start /MIN cmd /C c:\Python27\lib\idlelib\idle.py -e %1 %2 %3 %4 %5 %6
This can open up to six files from cmd line in one shot. Just type the name of the batch file, followed by from zero to six filenames. Also if one or more files you specify are not found, idle opens these as new document(s).
first make sure you have location of idle in path
I am using "python3.5".So mine looks like this:
C:\Program Files\Python35\Lib\idlelib.Yours may differ.
use this following command:idle -r file_name.py to run the file
or just idle file_name.py to edit
or start idle -r file_name.py ^&exit
you can just program in Python to edit your Python files. A simple example. say you want to search for a word and then replace it with something else.
import fileinput
import os
os.chdir( os.path.join("c:\\","path") )
for file in os.listdir("."):
for line in fileinput.input(file,inplace=0):
if "search word" in line :
line=line.replace("search word","new word")
print line
(use inplace=1 to do in place editing.). Then save and run the script as normal Python script using the interpreter.
Just add IDLE's path to your PATH environment variable.
For example I created an environment variable called IDLE_PATH and set the value to C:\Python27\Lib\idlelib
Then in my PATH variable I added ;%IDLE_PATH%; and open a new cmd prompt or in console2 just open a new tab and run idle <file_name> to open the file, you will be able to do this from any directory. In IPython console add an ! before the command, for example !idle test.py.
Congrates, Now you're a python pimp!
paste the idlelib to your system path or user path, environment variable.for example, like this
C:\Program Files\Python310\Lib\idlelib
then type idle in your command prompt. done.