Updated: TENSORFLOW Activating Virtualenv through Terminal is not Working - python

macOS High Sierra 10.13.2 — MacBook (Retina, 12-inch, Early 2016)
Trying to set up Python TensorFlow NOT because I like Python (hate it) but because that was what was recommended
NOTE: Not very experienced with Terminals and whatnot, but an okay programmer.
NEW PROBLEMS:
My computer seemed to have downloaded TensorFlow, but when following the steps to test it, this showed up:
Macbook:~ rose$ cd Desktop
Macbook:Desktop rose$ Python
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import tensorflow as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named tensorflow
EVERYTHING seemed fine when I finished the steps for installation...
So, I was thinking today that it would be pretty awesome to see if I could figure out how to set up some sort of neural network programming environment on my computer and eventually found TensorFlow. Everything was going pretty well, I was just following the instructions on this page at the official site for instructions. I installed pip, Virtualenv, nose/tornado (said I needed them, created a Virtualenv environment for Python 3.n, which I updated to today, then got to this step:
Activate the Virtualenv environment by issuing one of the following commands:
$ cd targetDirectory
$ source ./bin/activate # If using bash, sh, ksh, or zsh
$ source ./bin/activate.csh # If using csh or tcsh
I used the command it said to use for bash (wow, these names are horrible and nondescript), but I'm not 100% sure I use bash, almost certain because that is supposed to be the default on Mac and got this:
Input:
Macbook:~ rose$ source ./bin/activate
Error Message Output:
-bash: ./bin/activate: No such file or directory
NOTE: There were some other messages, here are screens of the whole shell.
Screenshots on my GitHub Pages

You haven't created a virtual environment. There were errors. Please reread carefully your own screenshots, last but one and the last.
Cannot copy/paste the error message here from the images. That's one of many reasons why images without text are bad.

Related

I downloaded Python 3.9 but nothing seems to have happened

Ok, I'm trying to install python 3.9 in my computer. The only thing I know about computers is programming, and I'm starting with Python. I really don't understand much of anywhere before that, so that's why I couldn't get what I'm looking for in other StackOverflow questions and so on. I have a hard time in that. Therefore, I'll describe exactly what I did:
I went to https://www.python.org/ and downloaded Python 3.9 (that button in the middle).
After it finished downloading, I opened it and went through the installation window. After it was done, I went to my Terminal (after I figured out what it was) and typed which python. This is where the problem starts; I got a pretty neutral return (just the path):
/usr/bin/python
After that, I did python. Then there was a problem:
WARNING: Python 2.7 is not recommended.
This version is included in macOS for compatibility with legacy software.
Future versions of macOS will not include Python 2.7.
Instead, it is recommended that you transition to using 'python3' from within Terminal.
Python 2.7.16 (default, Apr 17 2020, 18:29:03)
[GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc- on darwin
Type "help", "copyright", "credits" or "license" for more information.
All I understand is that the computer still has Python 2.7.16 installed (which, if I understand correctly, is a default for Mac), and not the 3.9. In the tutorial I was watching, this didn't happen... What can I do?
EDIT: By the way, the commands python3.9 and python3.9.0 gave the same error message:
>>> python3.9
File "<stdin>", line 1
python3.9
^
SyntaxError: invalid syntax
>>> python3.9.0
File "<stdin>", line 1
python3.9.0
^
SyntaxError: invalid syntax
Errors and lines beginning with >>> suggest you're still inside Python2 interpreter. You need to exit it before trying to run Python3. You can do that by pressing the Ctrl and d buttons on your keyboard at the same time, or typing exit().
After exiting, you should be able to run python3.
looks like your python is linked to python2.7 instead of python3.
just type python3 on commandline, this should solve your problem. you can check the version by typing python3 --version on commandline.
the same should work for pip3
The best easy way is to use anadonda based python or intel based.
Anaconda based python for MacOS
This should work for you.

Unable to import nltk on mac os x

I had successfully installed nltk from this site. And just to validate i am able to import it from the terminal. But when i execute my python script from the Spyder it gives me following error in Spyders terminal
File "/Prateek/Python/RC_ISSUES/algorithm_RC.py", line 9, in <module>
import nltk
ImportError: No module named nltk
Below output is from the terminal
I know there might be similar questions but i thought it is different from rest of the other questions
When you execute a python script, the operating system is looking for the interpreter as specified on the first line of the script, which most of the time is:
#!/usr/bin/python
On Mac OS X, this is the python as distributed with the system when you installed it, which is the one distributed with the system. It is usually very likely to be the one that has the older compilation date:
2.7.10 (default, Jun 1 2015, 09:45:55) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
If you do type python in your shell, you're very likely to see another path to that interpreter, e.g. if you installed the brew version of python:
% type python
python is /usr/local/bin/python
So you have two ways around it, either you explicitly launch your script with python:
python algorithm_RC.py
in doubt, use the full path you found out with type:
/usr/local/bin/python algorithm_RC.py
or, you can change your script first line with:
#!/usr/bin/env python
which will use the same python as the one you're reaching from your shell. You can also use the full path to your manually installed python, by making that line:
#!/usr/local/bin/python
or whatever the type command gave. But I would advise you against that, as the /usr/bin/env solution is more flexible and makes sure you're using in both cases the same python from the shell and within the script.
Finally, you can also use the system's python by calling explicitly easy_install from /usr/bin:
sudo /usr/bin/pip nltk
And if you don't have pip there, then you'll have to install it first:
sudo /usr/bin/easy_install pip
HTH
try 1:
>>>import nltk
and hit enter and now try
>>>nltk.download()

How to make Opencv 2.4.8 with Python 2.7.5 to work on Windows7 Cygwin64?

I spent some 15 hours by now trying to get Opencv 2.4.8 to work with Python 2.7.5 on my Windows7 Cygwin-64 environment.
After some struggle, I've finally managed to get Opencv work with C++, but I'm still not able to call Opencv from Python.
This is what I get when trying to import the cv2 module:
$ python
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv2
>>>
During the installation (with cmake-gui) of Opencv, all the configuration entries regarding Python were correctly defined.
I've added the path to opencv/bin to my PATH.
I've copied the file cv2.pyd from the opencv distribution to my Python installation's site-packages folder, to no avail.
I've tried to set PYTHONPATH to point to the site-packages directory, but it doesn't help.
To me, it seems like Python is unaware of the cv2 module.
Any pointers on what to try next would be very much appreciated. Also, if someone could provide some insights on how Python in general locates external non-Python modules, over and above placing the corresponding .pyd-file in the site-packages library, would be helpful for further trouble shooting.
TIA.
Problem solved! I'm now able to run Python with OpenCV.
#Yaakov set me on the right track: once I realized that the cv2.pyd is basically just a dll, the rest came easily: since I had built OpenCV from sources, I started to suspect that there just might be a cv2.dll having been built in that process. Indeed there was, in the lib directory of the OpenCV build-area.
After having copied that file to my python's site-packages directory, everything started working ok.
Additional details here.

PythonAnywhere + virtualenv: "Could not find platform dependent libraries <exec_prefix>..."

I have a Django (1.5.1) site running on Python 2.7.3 in a virtualenv at PythonAnywhere. As far as I remember, everything used to work fine. Lately, although I haven't changed anything except some Django code, I get the following message when I run pip:
(venv)11:34 ~ $ pip
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Traceback (most recent call last):
File "/*~*//venv/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
File "/*~*//venv/lib/python2.7/site-packages/distribute-0.6.34-py2.7.egg/pkg_resources.py", line 16, in <module>
import sys, os, zipimport, time, re, imp, types
ImportError: No module named time
Needless to say, pip does not work at all after producing the above error.
When I run python with the virtualenv activated, I again get the following error:
(venv)11:34 ~ $ python
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Python 2.7.3 (default, Apr 29 2013, 15:12:04)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "/*~*//.pythonstartup.py", line 1, in <module>
import rlcompleter
ImportError: No module named rlcompleter
>>>
But, thereafter, the Python console appears to be in working order.
Without the virtualenv activated, python runs fine without any errors.
All was well a couple weeks ago when I last ran pip and installed some packages, but now, pip is not working, although all of my Django site's features run just fine. I appreciate any ideas that could get pip to work again.
There's definitely something fishy going on if it can't find module time - as far as I'm aware that module is actually built in to the Python binary itself so it's not as if there's a time.py or time.so file which could have been deleted. It looks as if either Python in your virtualenv has become broken somehow, or there's something about the environment that's messing it up.
Setting a dubious PYTHONHOME variable can cause Python all sorts of trouble as it can't find necessary files, but I think in this case it's the fact that the virtualenv has become broken. In fact, I just tried an old virtualenv on PythonAnywhere myself and got the same issue. It looks as if the symlinks that virtualenv creates have become broken as the result of an upgrade, possibly the recent upgrade from Debian to Ubuntu.
If at all possible I would simply re-create a new virtualenv and run things from there. In principle you could use pip to write a requirements file so you can create a new virtualenv with exactly the same versions of the code, but the problem is that you can't run pip to create this file, QED. There's probably a clever way to use the system Python to run pip and get the dependencies from your virtualenv, but it's going to be tricky - it's not designed to work that way.
Alternatively you could contact the PA devs - I know that some users did have problems with their virtualenvs around the time of the upgrade and they may have cunning scripts which can fix the problem. Even if you just build a new virtualenv and use it, I would contact them and make sure they know about this issue so they're aware of it for future upgrades.
I had this happen to me this afternoon. I upgraded from Mint 14 to Mint 15, and it appears that the system python on the former is 2.7.3 and the latter uses 2.7.4. I fixed this by deleting my virtualenv (which used python 2.7.3) and then recreated it using python 2.7.4.
Check my quick screencast. In it, I show that:
the python environment is broken
recreating the python env makes it work
I get this error this day.Because i use virtualenv,and enter this,but ,my python script still contains this
#!/usr/bin/python2.6
delete this sentence will ok

PATH issues with homebrew-installed Python 2 and Python 3 on OSX

I'm relatively new to programming and have searched 'til my fingertips were blue, but can't seem to find a solution to the problem I'm having.
I have homebrew-installed versions of Python 2 and Python 3 on OSX and I can't seem to get the proper PATH/PYTHONPATH in my .bash_profile in order to be able to import modules properly in both versions in IDLE. I can, however, import modules when running Python 2 or Python 3 directly in a shell window. I am launching IDLE via terminal so it should properly initialize the paths.
Here is my .bash_profile:
export PATH=/bin:/usr/local/bin:$PATH
export PYTHONPATH=/Users/maverett/Documents/PyModules:/Users/maverett/Dropbox/matrix/:$PYTHONPATH
Here's what happens in all four cases, using numpy as an example module.
Importing when running python2 in terminal works:
$ python2
Python 2.7.5 (default, Jun 28 2013, 19:06:25)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
Launching IDLE for Python 2 from terminal, I can also import numpy. However, when I compare sys.path in IDLE vs sys.path in terminal, they are different. I compared the lists to generate the differences and found:
>>> InIdleNotInTerm
['/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/bin']
>>> InTermNotInIdle
[]
So there is one extra directory in the IDLE path when running Python 2.
The story is quite different for Python 3.
$ python3
Python 3.3.2 (default, Jul 1 2013, 10:53:26)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>>
However, if I launch IDLE for Python 3 (by typing idle3 in terminal) and then try to import numpy, I get
>>> import numpy as np
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import numpy as np
ImportError: No module named 'numpy'
>>>
Again, I compared sys.path in terminal and in IDLE and this time there are major differences:
>>> pp(InIdleNotInTerm)
['/Library/Frameworks/Python.framework/Versions/3.3/bin',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python33.zip',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages']
>>> pp(InTermNotInIdle)
['/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/distribute-0.6.45-py3.3.egg',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/pip-1.3.1-py3.3.egg',
'/usr/local/lib/python3.3/site-packages/distribute-0.6.45-py3.3.egg',
'/usr/local/lib/python3.3/site-packages/pip-1.3.1-py3.3.egg',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python33.zip',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages',
'/usr/local/Cellar/python3/3.3.2/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info',
'/usr/local/lib/python3.3/site-packages',
'/usr/local/lib/python3.3/site-packages/setuptools-0.6c11-py3.3.egg-info']
Any idea what's going on? A few other things I've tried:
Changing .bash_profile to
export PATH=/bin:/usr/local/bin:$PATH
export PYTHONPATH=/Users/maverett/Documents/PyModules:/Users/maverett/Dropbox/matrix/:/usr/local/lib/python3.3/site-packages/:$PYTHONPATH
results in being able to import modules in Python 3 in terminal and in IDLE, but breaks imports for Python 2 (because it tries to import the Python 3 versions!)
Launching IDLE for Python 3 via terminal from python3.3/site-packages/ results in being able to properly import everything, but I don't want to do this every time I launch Python 3.
Any thoughts or ideas you have would be greatly appreciated! Thanks :)
Ok, here's what I'd like you to do:
Stop using easy_install, if you're still using it for package management. Use pip instead.
$> easy_install pip
Next, get virtual environments.
$> pip install virtualenv
$> mkdir ~/venvs
$> virtualenv ~/venvs/numpy_project --python=python2.7 --no-site-packages
Make sure to pass in a well-named directory to virtualenv. The standard usage is to organize your virtual environments by project, so I named this virtual environment "numpy_project". You should probably come up with a better name. I also told it to use python 2.7, but you can choose to use 3.3 if you want.
Basically, this is going to create a well-insulated bubble for a dedicated copy of python to live for one specific purpose. I also told it to use no-site-packages, as to ensure a clean slate. It makes it easier to get set up for work on this project, which can be done by running:
$> source ~/venvs/numpy_project/bin/activate
This will switch your python environment from the global "main" python to this protected copy. You can then run:
$> pip install numpy
And it will install it just for that copy of python.
Be sure to run
$> pip freeze > requirements.txt
In the root of your project, where your README.md and stuff would go, so that others can simply run:
$> pip install -r PROJECT_ROOT/requirements.txt
And it will grab all the things you've put in your virtual environment (i.e., numpy). If they're using virtual environments as well, you can be certain they have an exact match to your working environment. There should be no overlap or confusion from other packages and versions of python.
Try this and see if you get better results. Remember to never run pip with sudo!
If this works, just uninstall your "global" install of numpy, and just use it in your virtual environments.

Categories