Python default site-package directory changed after installing Ruby - python

I have spent a few hours trying to solve this problem but didn't manage yet. I figure my question is a bit specific to my own mac configuration and I need a more detailed instruction.
So my problem is that I installed Ruby using home-brew, for some reason, this installation changed my default python site-package directory. Before I have Ruby, my terminal python will automatically call the site-package from the path '/Users/xxxxxx/canopy/lib/python2.7/site-packages'. However, it now will use '/Library/Python/2.7/site-packages'. I tried three different ways to solve this issue but all failed:
1 use Canopy Terminal like this Set Canopy Preference, but this method point to another directory '/Applications/Canopy.app/appdata/canopy-1.5.2.2785.macosx-x86_64/Canopy.app/Contents/lib/python2.7/site-packages', which is not i want either.
2 change the bash_profile like this amend path, but by just putting the # in every line of the bash_profile, it still use directory '/Library/Python/2.7/site-packages'. Btw, my bash_profile and $PATH looks like this before:
and looks like this
3 add the path '/Users/xxxxxx/canopy/lib/python2.7/site-packages' into sys.path, which look like this now:
But none of those worked. How can I make my terminal point to the path '/Users/xxxxxx/canopy/lib/python2.7/site-packages' now? Or ultimately, what should I do to clean up those different site-packages and use the one I only need?
Thank you for any help!

On a mac by default the path is /Library/Python/2.7/site-packages/foo.egg.
Before doing anything, in terminal, start a new Python session and do
import sys
print sys.path
If the paths are of the form /Library/Python/2.7/site-packages/foo.egg, then just follow link
tl;dr
Basically add this to your bash_profile,
source [...] /activate
VIRTUAL_ENV_DISABLE_PROMPT=1 source [...] activate

I found the solution:
added to the bash_profile this line " export PATH="$HOME/canopy/bin:$PATH" ", and uncomment all other lines in bash_profile.

Related

Why would I add python to PATH

I am beginning to look at python, so when I found a tutorial it said that the first thing to do would be to download python from www.python.org/downloads/
Now when I downloaded python 3, I then started the installation and got to
Why would I want to "Add Python 3.5 to PATH"? What is PATH? Why is it not ticked by default?
PATH is an environment variable in Windows. It basically tells the commandline what folders to look in when attempting to find a file. If you didn't add Python to PATH then you would call it from the commandline like this:
C:/Python27/Python some_python_script.py
Whereas if you add it to PATH, you can do this:
python some_python_script.py
Which is shorter and neater. It works because the command line will look through all the PATH folders for python and find it in the folder that the Python installer has added there.
The reason it's unticked by default is partly because if you're installing multiple versions of Python, you probably want to be able to control which one your commandline will open by default, which is harder to do if both versions are being added to your PATH.
In addition to what #SuperBiasedMan stated, you can edit your PATH in Windows by hitting Start > Run, then type sysdm.cpl.
From there, navigate to Advanced tab and then hit Environment Variables.
In the lower section, where it says 'System variables', find the one named PATH and double click it. Note that it would be easier to copy and paste it to a notepad or something. The separator as you can see is a semi-colon.
Any path that you add to this variable, will be looked when you type any command in a cmd window or through the 'Run' command line.
That's the same concept as in Linux, I just pointed out how it can be edited.
This shows the way if you haven't add python to PATH(By the way, the python.exe is in my Python directory)
This is the way if you add python to a PATH

sys.path doesn't include PYTHONPATH (must type full path to run django-admin startproject)

I'm running Windows 8.1, Python 3.4.2, Django 1.7.
.py files are associated with python.exe.
Windows system path is:
C:\lots_of_paths;C:\Python34;C:\Python34\Lib\site-packages\Django-1.7.1-py3.4.egg\django\bin
(total of 531 characters)
PYTHONPATH variable in registry is:
C:\Python34\Lib;C:\Python34\DLLs;C:\Python34\Lib\site-packages\Django-1.7.1-py3.4.egg\django\bin
(total of 96 characters)
I pasted the Django path when setting this variable, to avoid typos. When I copy and paste from PYTHONPATH to notepad there is not a break in the line, so there appears to be no hidden carriage return.
I start cmd.exe and work from the directory C:\projects.
When I enter python -c "import sys;print(sys.path)"
The result is:
['', 'C:\Python34\lib\site-packages\django-1.7.1-py3.4.egg', 'C:\WINDOWS\S
YSTEM32\python34.zip', 'C:\Python34\DLLs', 'C:\Python34\lib', 'C:\Python34
', 'C:\Python34\lib\site-packages']
\django\bin is being clipped from the path.
When I enter python django-admin.py startproject please_work
...it prints: python: can't open file 'django-admin.py': [Errno 2] No such file or directory
It appears that system path locates Python, but PYTHONPATH doesn't work, as expected.
When I type django-admin startproject saving_time I get this:
Usage: django-admin.py subcommand [options] [args]
Options:
-v VERBOSITY, --verbosity=VERBOSITY
Verbosity level; 0=minimal output, 1=normal output,
2=verbose output, 3=very verbose output
(etc. etc. etc.)
...so it appears that django-admin.py is found on the system path and ran via association with python.exe, but the arguments are not passed. I get the same result when the .py suffix is included in the command as suggested in one answer below: django-admin.py startproject testproject
Typing python C:\Python34\Lib\site-packages\Django-1.7.1-py3.4.egg\django\bin\django-admin.py startproject wasting_time functions properly, but it shouldn't be necessary, right?
Since you do have a command line solution that works why not create a batch file in C:\Python34\Scripts possibly called dj-admin.bat which includes either your full command with parameters, if this is the command that you do all of the time, or allows you to use parameters.
By the way, as far as I know, when you type:
python somefile.py [parameters]
Python expects somefile.py to be either in the local directory or an absolute path it doesn't search PYTHON_PATH for it but if you type:
python -m somemodule [command_from_module] [parameters]
Python will look first in the current directory then on PYTHON_PATH.
The other possible source of your problem is having more than one django-admin on your path - if it is earlier in the path it will take precedence. On Unix/Linux systems there is a lovely command called which which will tell you which commands are being found.
A python implementation of which is quite simple to do.
django-admin.py startproject testproject should be enough
since django-admin.py resides on systempath
I suggest you to use virtual environments instead of globalizing the packages
This problem was a result of downloading and installing Django from the .egg compressed file instead of using pip.
I'm not sure how it traces to the source, but that was the problem. When Django was installed using pip, this problem disappeared.
Warning: The remainder of this text is the type of emotional human expression upon which stackoverflow so scornfully frowns, the sharing of which I cannot resist.
It has been months since I stumbled upon the solution and I just now remembered to update this thread.
Thank you to Steve Barnes for teaching me to think outside of the box and find a temporary solution. Thank you to Nishan-Paradox for adding that suggestion to use virtual environments. Suggestions like those are really valuable to a newcomer, even if they don't pertain to the question. Common sense is still in vogue.
A side note: I am now using pip and virtual environments religiously, and life is much better :) To any other newbies who may read this: If you see many others using a tool, try it out before you decide if you want to use it or not. A Google search may not give you enough information to make the decision, but experience will. You may be delighted to try something new and add it to your toolbox.

python is not recognised as an internal or external command [duplicate]

This question already has answers here:
"python" not recognized as a command
(14 answers)
Closed 2 years ago.
This is a really annoying problem. I've prowled the web for solutions, but all I found was tips about changing the PATH variable, which I did, of course. My Python install directory is C:\Python27. It' a 32 bit version. Whenever I type python in the command prompt, it says that it isn't recognised as an internal or external command. Currently, my PATH variable is set to C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts. Anyone has any ideas? I run Windows 7 by the way (64 bit). I'm pretty desperate. Heck, if nothing works I guess I'll try dual-booting Linux and Windows 7...
Just adding the Python path and trying again worked for me (without reboot).
MS-dos command for Python 2.7:
set PATH=%PATH%;C:\python27\
MS-dos command for Python 3.3:
set PATH=%PATH%;C:\python33\
(check if that is the folder where you installed Python).
Quick fix: May not be the most elegant or long term fix but if you are really frustrated and just want to get it to run, just copy paste the python.exe file to your current directory. This worked for me.
After adding the python folder to the system PATH variable, you should reboot your computer.
Another simple solution is: create a shortcut of the python.exe executable (probably it is in C:\Python27\python.exe, or similar) in a place like C:\Windows\system32 (that is, a place that already is listed in the PATH variable). The name of your shortcut should be python (maybe python.exe should work too). I mean, it can't be python - shortcut or similar, for your purposes.
To see the contents of the PATH variable, go to the cmd and enter set PATH.
After changing the PATH variable in windows, you need to reboot your system before it takes effect.
Edit: As stated by #tdelaney, only a restart of cmd.exe should be required. This is true atleast for Windows 7 64bit.
In your PATH have you got a leading space before your Python path?
For example:
"C:\somedirectory\bin; C:\Python27;C:\Python27\Lib\site-packages\;C:\Python27\Scripts"
If you have a leading space between path's "ry\bin; C:\Pyt", it won't work and is usually the cause for this type of issue.
When installing, there is a checkbox that is by default not selected, but it asks to add python to the environment variable. Re-install and check that box.
I'd rather the installer do it than struggle in the weeds myself.
Okay, as you said your Python install directory is C:\Python27, open my computer, then open c: drive,
if you don't see "Python27" named folder there then try to search it using search option, (in my case i found it in old.window folder, don't know how it moved there) cut and past it in c drive along with folders like, program files, user etc... , now open cmd and type python and hit enter to check if it is working now,
This is only a partial answer, but I found (repeatedly) that I'd have similar issues when I would use the gui installer and not go through the custom setup.
Using the custom setup option, then using the same settings, the "install for all users" (that then installs to C://python.version/blah instead of the user based default structure) WOULD allow the installer to setup PATH correctly.

Add a directory to Python sys.path so that it's included each time I use Python

Currently, when trying to reference some library code, I'm doing this at the top of my python file:
import sys
sys.path.append('''C:\code\my-library''')
from my-library import my-library
Then, my-library will be part of sys.path for as long as the session is active. If I start a new file, I have to remember to include sys.path.append again.
I feel like there must be a much better way of doing this. How can I make my-library available to every python script on my windows machine without having to use sys.path.append each time?
Simply add this path to your PYTHONPATH environment variable. To do this, go to Control Panel / System / Advanced / Environment variable, and in the "User variables" sections, check if you already have PYTHONPATH. If yes, select it and click "Edit", if not, click "New" to add it.
Paths in PYTHONPATH should be separated with ";".
You should use
os.path.join
to make your code more reliable.
You have already used __my-library__ in the path. So don't use it the second time in import.
If you have a directory structure like this
C:\code\my-library\lib.py and a function in there, e.g.:
def main():
print("Hello, world")
then your resulting code should be
import sys
sys.path.append(os.path.join('C:/', 'code', 'my-library'))
from lib import main
If this is a library that you use throughout your code, you should install it as such. Package it up properly, and either install it in your site-packages directory - or, if it's specific to certain projects, use virtualenv and install it just within the relevant virtualenvs.
To do such a thing, you'll have to use a sitecustomize.py (or usercustomize.py) file where you'll do your sys.path modifications (source python docs).
Create the sitecustomize.py file into the \Lib\site-packages directory of your python installation, and it will be imported each time a python interpreter is launched.
If you are doing this interactively, the best thing to do would be to install ipython and configure your startup settings to include that code. If you intend to have it be part of a script you run from the interpreter, the same thing applies, since it will have access to your namespace.
On the other hand, a stand alone script should not include that automatically. In the future, you or some other maintainer will come along, and all the code should be obvious, and not dependent upon a specific machine setup. The best thing to do would be to set up a skeleton file for new projects that includes all of the basic functionality you need. That, along with oft-used snippets will handle the problem.
All of your code to run the script, will be in the script, and you won't have to think about adding that code every time.
Using jupyter with multiple environments, adding the path to .bashrc didn't work. I had to edit the kernel.json file for that particular kernel and append it to the PYTHONPATH in env section.
This only worked in that kernel but maybe this can help someone else.

remove item from python path

I have added a path to the system pythonpath on linux and now i've broken it. How may I remove it ?
[EDIT]
Finally i solved it removing the script that added that path + installing something to rebuild the path.
not directly programming related but....
'print' your pythonpath to a file, edit the file and export that as your new pythonpath
[edit]
I stand corrected, see Banang answer below
[/edit]
I don't think there is a concept of Windows-like environment variables in Linux; they are defined in various scripts (e.g. .bashrc). You can edit those in any text editor.
What exactly did you to do "break" your PYTHONPATH?

Categories