Installed a package with Anaconda, can't impor

2020-07-02 08:54发布

问题:

Forgive me but I'm new to python. I've installed a package (theano) using conda install theano, and when I type conda list, the package exists

However, when I enter the python interpreter by running python, and try to import it with import theano, I get an error: "no module named theano", and when I list all python modules, theano doesn't exist.

What am I missing?

回答1:

Probably due to the fact you have multiply python envs installed in your computer. when you do which python you will probably get the native python installed in your computer. that is /usr/bin/python

You want to use the Python that came when you installed Anaconda. Just add Anaconda path to the beginning of your $PATH. (In order to do this you probably need to edit your ~/.bashrc file (or the equivalent file for your shell) then source ~/.bashrc.

Next time you will go to will run python and import theano you'll succeed.



回答2:

When I had this issue my python install was actually missing a "site-packages" path reference. To solve/workaround the issue do the following.

  1. Search for your newly installed package from the Anaconda directory and note the path. (e.g. C:\Anaconda\site-packages)
  2. Run the following in your terminal:
        python -c "import site; print(site.getsitepackages())"

Example Output: ['C:\Anaconda3', 'C:\Anaconda3\lib\site-packages']

  1. If the path noted in step one is missing from the list then that's your problem. The quick fix is to move the new package to a listed site-packages folder or add the missing path to your PYTHONPATH environment variable.

If you're interested in managing your own "site-packages" locations check out the Python Doc for details on setting up a site config file.



回答3:

Do you have another installation of Python on your system? You can run "which python" in your terminal to determine which Python will be used.



回答4:

So I also had the same problem, turn out that I had named my own file to the same modulename (graphviz) and it tried to import that one instead... Took me a while before I figured that one out!



回答5:

I had a base environment where I had installed keras_vggface using conda (sudo pip install git+https://github.com/rcmalli/keras-vggface.git: Courtesy: https://machinelearningmastery.com/how-to-perform-face-recognition-with-vggface2-convolutional-neural-network-in-keras/). Launched anaconda-navigator from base (post conda activate base), import keras_vggface failed.

When base is deactivated, and in python command line, import worked fine. which python reveals the one within anaconda bin directory. Now, I did pip3 install keras_vggface while being in base.

Now, I am able to import the module from within base and in python prompt and also from jupyter notebook launched from base via anaconda-navigator.

Note: this is not an expert advice on how it has to be done; please use this experience with pinch of salt.



回答6:

I had this problem and realised that the issue was that ipython and jupyter-notebookdid not have the same sys.path as python, just in case that helps anyone.