setting/changing PYTHONPATH in OS Mavericks - python

Ok, I hate to start another of seemingly hundreds of other threads about this, but how do I set or tweak the PYTHONPATH? I'm very new to Mac and the only other time I've successfully done this was on Windows using Rapid Enviroment Editor. This looks like a whole different beast. I tried installing Django and it failed to do it for 2.7, while I need 3.4, so the current path must be for 2.7. Running which python in Terminal shows the /usr/bin/python directory, which I then can't cd into, let alone find by browsing. I see that my Python 3.4 directory has the Update Shell Profile file, but it has a lot of limitations. I also see other threads mention PYTHONPATH commands in IDLE and creating one of the bash profile type files for the Terminal. How can I set this and not worry about it anymore until I need to run a different version of Python? I'm on Mac 10.9.2.
"Explain like I'm five".

If you want to install packages for python 3.4 use: pip3 install django
When installing for python 2.7 just use: pip install django
To use python 3.4 type python3 in your shell.
To see where all installations of python are use: which -a python
Depending on how you installed the new versions of python you will see output like:
/usr/local/bin/python
/usr/bin/python
If you wanted to use the python in /usr/local/bin/python you can edit your .bashrc file and add export path="/usr/local/bin:$path".
Save, then type source .bashrc in your shell and when you type which python it will show something like /usr/local/bin/python
Don't screw around too much with different versions of python, you will end up causing yourself a lot of problems.
You should not have to change your PYTHONPATH, just specify which python or pip version you want to use and that will most likely be all you need to do.

To update PYTHONPATH you can run from the terminal:
export PYTHONPATH=$PYTHONPATH:/desired/path/to/add
Then to check the updated PYTHONPATH you can run:
echo $PYTHONPATH
I'm not sure if this completely answers your question, but this is one way to make sure modules are visible to python when you import them.

Related

python27 and python3 are not not recognized as an internal or external command, operable program or batch file [duplicate]

I am using Python 3.5.2 version on Windows 7 and tried using python3 app.py. I am getting this error message:
'python3' is not recognized as an internal or external command,
operable program or batch file.
Is there any specific cause about why the python3 command is not working?
I also verified that the PATH is added to environment variables.
There is no python3.exe file, that is why it fails.
Try:
py
instead.
py is just a launcher for python.exe. If you have more than one python versions installed on your machine (2.x, 3.x) you can specify what version of python to launch by
py -2 or
py -3
You can also try this:
Go to the path where Python is installed in your system. For me it was something like C:\Users\\Local Settings\Application Data\Programs\Python\Python37
In this folder, you'll find a python executable. Just create a duplicate and rename it to python3. Works every time.
Python3.exe is not defined in windows
Specify the path for required version of python when you need to used it by creating virtual environment for your project
Python 3
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
Python2
virtualenv --python=C:\PATH_TO_PYTHON\python.exe environment
then activate the environment using
.\environment\Scripts\activate.ps1
Yes, I think for Windows users you need to change all the python3 calls to python to solve your original error. This change will run the Python version set in your current environment. If you need to keep this call as it is (aka python3) because you are working in cross-platform or for any other reason, then a work around is to create a soft link. To create it, go to the folder that contains the Python executable and create the link. For example, this worked in my case in Windows 10 using mklink:
cd C:\Python3
mklink python3.exe python.exe
Use a (soft) symbolic link in Linux:
cd /usr/bin/python3
ln -s python.exe python3.exe
In my case I have a git hook on commit, specified by admin. So it was not very convenient for me to change the script (with python3 calls).
And the simplest workaround was just to copy python.exe to python3.exe.
Now I could launch both python and python3.
If python2 is not installed on your computer, you can try with just python instead of python3
For Python 27
virtualenv -p C:\Python27\python.exe django_concurrent_env
For Pyton36
virtualenv -p C:\Python36\python.exe django_concurrent_env
Enter the command to start up the server in that directory:
py -3.7 -m http.server
I had a related issue after installing windows 11, where python3 in cmd would open the windows store. I was able to sort it out between this post and this other one. In short, I reinstalled python and made sure to add it to PATH. Then, in settings, Apps > Apps & Features > App Execution aliases. Here, all I had to do was make sure that every single python .exe (including idle and pip) were turned off EXCEPT FOR the python3.exe alias. Now it works like a charm.
FWIW:
The root of this issue is not with you or with python. Apparently, Microsoft wanted to make installing python easier for young kiddos getting interested in coding, so they automatically add an executable to PATH. For those of us that already have this executable, it can cause these issues.
Found out instead press the play button the top right and it should work in visual studios:
Do not disable according to first answer
Saying python3 in the command will not work by default.
After figuring out the problem with the modules (Solution): https://youtu.be/paRXeLurjE4
Summary:
To import python modules in case of problem to import modules:
Hover over python in search:
Click open in folder
Hover over and right click
click properties
copy everything in path before \python.exe
close those windows
For cmd (administrator):
cd --path that was copied--
then python -m pip install --upgrade pip
cd Scripts
pip install "Name of Package" such as pip install --module (package) --
Im on win10 and have 3.7, 3.8 and 3.10 installed.
For me "python" launches version 3.10 and does not accept commands (like -3.7), "py" launches newest version but does accept commands, and "python3" does nothing.
Uninstalled 3.10 and "python" now does nothing, and "py" launches 3.8.
I am unable to add a comment, but the mlink option presented in this answer above https://stackoverflow.com/a/55229666/8441472 by #Stanislav preserves cross-platform shebangs at the top of scripts (#!/usr/bin/env python3) and launches the right python.
(Even if you install python from python.org, Windows will direct you to the app marketplace nowadays if you type python3 on the command line. If you type python on the same cli it will launch the python.org version repl. It leads to scripts that generate no output, but more likely silently failed completely. I don't know ho common this is but have experienced it on a couple of different devices)
If you have this at the top of your script to ensure you launch python3 and don't feel like editing everything you own, it is not a bad approach at all... lol.

Installed Python 3.7 and tried changing that to the default however, changing $PATH and running python --version still shows 2.7

Sorry if the question is simple or I'm missing something obvious but I'm fairly new with console commands and Python.
As the title suggests, I installed Homebrew and subsequently the latest version of Python. I want to make this the default when running python --version in bash.
I tried changing the path in my bash_profile with the following line at the bottom:
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
It seems that when I install new modules however, that they are for 2.7 and not 3. Checking pip --version going straight to the default:
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
I also tried running the following commands:
echo "export Path=/usr/local/bin:$PATH" >> ~/.bash_profile && source ~/.bash_profile
this does edit my bash_profile again, but the line I wanted (and added manually) isn't the one that appears; instead the following is inserted (I have no idea why Applications/VMware is inserted at the end, it has nothing to do with Python or what I'm trying to do):
export PATH=/usr/local/bin:/usr/local/sbin:$PATHexport Path=/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware
Can anyone explain to me how I can make python3 the default. I don't want to get rid of 2.7 obviously because a lot of scripts for mac still depend on it. Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
You really don't want to change this.
First:
Would it be possible to install pipenv and run 3.7 from there, while keeping 2.7 at the default?
Yes! Install pipenv, and set up an environment where 3.7 is the default, but leave the system default at 2.7.
More generally:
According to PEP 394, which isn't due to be reevaluated until 2020, if you don't have a virtual environment activated:
python3 runs Python 3.x
python2 runs Python 2.x
python usually shouldn't be used—but if you do use it, it should run Python 2.x.
Usually, you'll also have, e.g., python3.7 and python2.7 to run specific 3.x and 2.x versions, although this isn't covered by any standard.
Scripts like pip, and those installed by pip, should be installed with similar suffixes—but ideally, if you have multiple versions, you shouldn't run those scripts. Most of them are designed to be run as modules, so you can run python3 -m pip to run the pip for your 3.x.
For a few things, it may be handy to set up shortcuts. (For example, instead of running python3 -m ipython from the command line, I usually use an iTerm profile that uses that instead of bash as my start command. If you don't use either IPython or iTerm, you can make a Terminal.app profile that runs python3.)
Scripts installed as part of your platform's package manager are a whole more complicated story, but macOS doesn't have an official package manager, and Homebrew generally expects you to install Python libraries with pip, not brew, so you don't have to worry about that.
Programs should have an installer—whether they install via pip or something like py2app—that creates a proper shbang line, so they run with whichever Python version they were installed with, so you don't have to worry about them. But programs that come with your OS may use something like /usr/bin/env python, and they will expect that to run the version of Python that came with your OS.
For development purposes, you usually want to use virtualenv or pipenv or conda so that python runs the Python interpreter for whichever environment is currently activated.
So, don't try to change the fact that python runs Apple's Python 2.7. That's what it's supposed to do, and if you make it do otherwise, you could break things. Use python3, python3 -m pip, etc.; use #!/usr/bin/env python3 on your scripts (or, better, use setuptools to create entry-point scripts automatically); use virtual environments; just never run python, and everything will work properly.
You just need to run command
$which python
And then goto the path rename python to python2
Then run command
$which python3
To get the path of python3
Just link with command
sudo ln -s $python3path /usr/bin
/python
You need to instead $python3path with real path
Then you can run python3 with python command

path to /opt/python3.5.0/bin for the usage of python3

I installed python3.5.0 in /opt/python3.5.0/bin/, but I couldn't use it beyond this bin folder. I know this is a path issue. Could somebody point out the correct procedure to make python3 show up in the system for use? The operating system is CentOS6.6
The os needs to know where your python executable is.
Try running it from somewhere else
cd
/opt/python3.5.0/bin/python -V
If that works the your Python3 installation in opt is ok and you just need to let your shell (probably bash) know where the python binary is by changing your PATH environment variable.
export PATH=/opt/python3.5.0/bin/:$PATH
Run that line on the bash terminal and try running python again with no path like this:
python -V
If that works put the export PATH=/opt/python3.5.0/bin/:$PATH line in your .bashrc or .bash_profile so it will work in the console in future.
Failing all that take a look at setting the PYTHONHOME and PYTHONPATH environment variables.
Also a better approach might be to install python3 using your package manager. The python3 executable should give you a version of Python-3 if you have one installed. Though you might get a different version of Python3 than 3.5 that way.

Conflicting Python Installs in OSX Mavericks

I've got myself in a pickle and would like some guidance before my laptop suffers GBH.
I have been using my Macbook for a few years without probs but when I got a new iMac at work I noticed everyone recommended useing homebrew for new Python installs (esp. on Mavericks).
Now my laptop has worked fine with the original Python. But I decided to try and do it the new homebrew way (its now Mavericks btw).
First I clear out Mavericks due to some other conflict then reinstall a fresh. Get nginx, php-fpm, mysql, etc working.
Now Python.
I can use pip to install packages. But when I try
workon myproject
I get:
/usr/bin/python: No module named virtualenvwrapper
/usr/bin/python: No module named virtualenvwrapper
But then it switches to that virtualenv anyway - but not the directory that the project is in!
When I run where python I get several entries:
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python
I've tried fiddling with my path but keep getting other errors that all seem to indicate my two Python installations are conflicting badly. Is there a simple solution or do I need to wipe out hombrew and start again from the beginning? Or just give up on homebrew and use OSX built in?
To clarify my current $PATH is:
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/op‌​t/X11/bin:/usr/local/go/bin:/Users/me/Development/Android/sdk/tools:/Users/me/Dev‌​elopment/Android/sdk/platform-tools
My solution was an unusual one but here goes:
In my .zshrc file I had the following layout:
ZSH=$HOME/.oh-my-zsh
ZSH_THEME="steeef"
plugins=(osx virtualenv virtualenvwrapper python github)
source $ZSH/oh-my-zsh.sh
# various aliases
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
The problem was the Oh My ZSH plugin virtualenvwrapper was looking in the default python path location as the custom path had yet to be set later in the .zshrc file.
The solution therefore was to move the PATH declaration before the plugins like so:
ZSH_THEME="steeef"
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
plugins=(osx virtualenv virtualenvwrapper python github)
source $ZSH/oh-my-zsh.sh
# various aliases
Now the ZSH plugins are referencing the correct Python install and therefore the correct Python packages path.
Try putting your preferred version earlier in your PATH variable. So if you want to use /usr/local/bin/python, from the command line you could modify your PATH by export PATH=/usr/local/bin/python:$PATH. See here for more info: http://www.cyberciti.biz/faq/appleosx-bash-unix-change-set-path-environment-variable/

Python - Virtualenv , python 3?

Seems everyone recommends virtualenv for multiple python versions (on osx), but does it even work with python 3.0? I downloaded it, and it doesn't seem to.. And I don't really understand how it works, Can you 'turn on' on env at a time or something? What I want is to leave the system python 2.5 (obviously), and to have python 3.1.1 with subversion pygame to write my own stuff, and python 2.6 with normal stable pygame to use to run other things, like pygame games downloaded from pygame.org. Any help on how to accomplish that? Thanks.
OK I realized virtualenv is not what I'm looking for.
It's an old question by now, but I found it myself on top of google search for the answer, and I don't think the answers provided are what people are looking for.
As I understand it you want to create different virtual environments with different Python versions?
This is very easy, and you only need virtualenv itself.
For, say, a Python 3:
$ virtualenv -p python3 p34env
(...)
New python executable in p34env/bin/python3.4
Also creating executable in p34env/bin/python
Installing setuptools, pip...done.
$ source p34env/bin/activate
(p34env)$ python -V
Python 3.4.2
(p34env)$ deactivate
$
You use the source command to activate the venv, and deactivate to - you guessed it - deactivate it. Notice the prompt changes to indicate the env.
For your system's standard version of Python you just skip the -p python3 argument, and you can use the argument to point to any version you want given a path.
The last argument is the name (p34env) and you can make as many as you like, just give them different names.
Your use case doesn't actually need virtualenv. You just need to install several different Python versions.
virtualenv is designed to create isolated environments of a Python environment. The trick to using it with multiple Python instances is to either install virtualenv into each of the Python versions you want to use it with, for example:
/usr/bin/easy_install-2.6 virtualenv
/usr/local/bin/easy_install virtualenv
sudo port install py26-virtualenv
or to invoke it with the intended Python version, for example:
/usr/bin/python2.6 virtualenv.py ENV
/usr/local/bin/python2.6 virtualenv.py ENV
/opt/local/bin/python2.5 virtualenv.py ENV
So, as such, it doesn't directly solve the problem (particularly acute on OS X) of which Python you want to work with. There are various ways to deal with that issue: use absolute paths to the intended Python (as in the above examples), define shell aliases, carefully manage the $PATH search order, among others.
At the moment, AFAIK, virtualenv is not supported with Python 3 because, among other things, setuptools (the magic behind easy_install) is not yet supported on Python 3, although there is work in progress towards a solution for that.
BTW, many people use Doug Hellman's virtualenvwrapper to simplify use of virtualenv.
For me virtualenv3 works very well. I also have pypi.python.org/pypi/distribute installed. That also works with the mentioned www.doughellmann.com/docs/virtualenvwrapper/ . I've only tested that on Linux though.
Not sure if I understood you correctly, but here goes :)
I don't know about OS X, but in Linux you can install both 2.6 and 3. Then you can either specify to use python25 or python3, or change the /usr/bin/python symlink to the version you want to use by default.

Categories