Cannot find modules after installing Anaconda - python

I have installed Anaconda but still unable to use packages such as pandas and requests when running code on python.
When I input python on Terminal, it shows:
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
And I also created a virtual environment and activate before opening the python shell:
conda create -n my-env python=3.6 anaconda
However, when reopening and running codes on Python, it kept saying "No module named 'pandas' or "No module named 'requests'" Does anyone know what the problem with this code is? Besides, I am not if version of Python has anything to do with this error? Is it because I am using 3.6.2 Python instead of 3.6.1?

Probably Anaconda became the default Python installation on your system. Specially when you are using a new environment, you have to install the modules you want in this new environment.
Try conda install pandas and, after that, import pandas

Related

Terminal shows wrong Python version as default

Typing Python on my Terminal shows
Python 3.9.5 (default, May 4 2021, 03:36:27)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
When in fact I have installed the latest version, 3.10.6, which shows when I type python3 --version. Typing just python --version returns 3.9.5 again.
It seems that the default version is 3.9.5 and not updated, or am I wrong? Either way, how do I solve this? I've tried both downloading and installing it directly, as well as through brew install ..., both giving successful installs, but not updating the default version (3.9.5 -> 3.10.6)
The solution, as provided in the comments, was to create a ~/.zshrc file (How to create a ~/.zshrc file) and then to replace the existing path with export PATH="$(brew --prefix python#3.10)/libexec/bin:$PATH"

default python won't switch to homebew installation

I know this question has been asked - but none of the solutions there seem to be working. I recently found out that on Mac OS, the native installation of python isn't very good. So, I installed homebrew, and installed Python 2 through there. My path is as follows:
~ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin
However, I still get the following:
~ which python
/usr/bin/python
~ which python2
/usr/local/bin/python2
It's also probably important to note that my ~/.bash_profile, ~/.profile and ~/.zshrc files are empty.
Somehow, I think I got pip to work with the python2 (homebrew) installation. Basically, I did pip install pandas and I get the following outputs for the two python interpreters:
~ python
Python 2.7.10 (default, Feb 7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
That is, it doesn't appear with the default python installation. However, I also have the following:
~ python2
Python 2.7.14 (default, Sep 25 2017, 09:53:22)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>>
So, pandas is imported in the python2 (homebrew) python, but not the standard one. How can I get it so that the default python is my homebrew python? I also read somewhere that brew link python would work, but when I do that I get
~ brew link python
Warning: Already linked: /usr/local/Cellar/python/2.7.14
To relink: brew unlink python && brew link python
I want to be able to just use python my_file.py and have it use the homebrew installation. (Not sure if it's related, but hopefully this would also allow Sublime to use the homebrew install when I hit command + B).
Sorry for the long post, new computer and I'm trying to get this all correct before I do any big projects and find out halfway through that things aren't working the way I need them to.
Thanks!
The macOS system Python is installed at /usr/bin/python. This is the only one called python on your path.
Homebrew doesn't create a link for python in /usr/local/bin. If you want one you can create it yourself:
ln -s /usr/local/bin/python2 /usr/local/bin/python
Alternatively, just update your shebang lines to specify python2 or python3, which is probably better anyway (explicit is better than implicit).

python-Can't import module in Pycharm while I can import the same module in terminal?

I installed essentia by
brew install essentia --HEAD
message:
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
mkdir -p /Users/yangyy/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")' >> /Users/yangyy/Library/Python/2.7/lib/python/site-packages/homebrew.pth
I did as the message instructed and installed essentia successfully.
The module can be imported in terminal:
Python 2.7.11 (default, Feb 23 2016, 00:59:46)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
-import essentia
-exit()
$ which python
/usr/local/bin/python
I use Pycharm as my IDE. I tried all the interpreter, but still can't import this module.
In python, I tried:
>>> import essentia
>>> reload(essentia)
<module 'essentia' from '/usr/local/lib/python2.7/site-packages/essentia/__init__.pyc'>
Pycharm wont take package dependencies instead it contains package you should initiate for your projects..
so, for that, You should go to
settings -> project Interpreter
then choose your project and check your packages are there then click,
apply <-button
that's it

Is it possible to use ipython based on a builtin python

I am using paraview 4.3.1 in Centos 7. There is a built-in python named pvpython:
Python 2.7.2 (default, Jan 15 2015, 09:36:49)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys;sys.path
['', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/site-packages/vtk', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/site-packages', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/lib/python27.zip', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/lib/python2.7', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/lib/python2.7/plat-linux2', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/lib/python2.7/lib-tk', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/lib/python2.7/lib-old', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/lib/python2.7/lib-dynload', '/tmp/ParaView-4.3.1-Linux-64bit/lib/paraview-4.3/lib/python2.7/site-packages']
>>>
The problem is, original pvpython do not support many useful features such as code completion. And there is no setup_tools in the pvpython, so I cannot install new modules to the built-in python.
Could anyone help me to install ipython on this built-in python?
See the install instructions for get-pip:
https://pip.pypa.io/en/latest/installing.html
However, you must use the pvpython instead of python when using the install instructions.
Depending on how you install it, you may end up with more than one easy_install or pip on your path. Just make sure you're using the one associated with pvpython - look at the script you're running to find out (it's just a shell script).
Once you've done that, installing ipython should be the same as installing it with any other version of python (assuming that pvpython doesn't break anything that ipython needs).

Multiple Python Version Multiple Modules

I have installed on Mac OS X 10.10:
I have installed Python 2.7.6 default apple
2.7.9 python mac ports
and
Pypy.
When i type pip freeze there a lot of modules installed.
However i can import some modules on one python and not the other one, because not everybody finds all modules.
Can someone explain me how can i achieve that every python version find every installed module?
(and does pip show all installed modules?)
As far as i can tell all modules are at : /Library/Python/2.7/sites-packages. But i don't know why not every python looks there.
How about using virtualenv to manage multiple python? https://virtualenv.pypa.io/en/latest/
If you are not sure about which module you are importing, you could try to print the file of the module ( if the module has this attribute). eg:
macbook:~ mac$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt4 as f
>>> print f.__file__
/Library/Python/2.7/site-packages/PyQt4/__init__.pyc
python will search the PYTHONPATH setting and try to load the module, if there are multiple python module with same name, it actually loads the 1st one.

Categories