Packages imported in Jupyter Notebook not working in Anaconda Prompt - python

I am importing libraries which I installed using pip install .. to Jupyter Notebook using the anaconda distribution, which is working.
Next I am tryin to import the same libraries in VS Code and running it using Anaconda prompt. I am getting error messages like: ModuleNotFoundError: No module named 'spacy'
I don't understand why it would work in Jupyter notebook but not using VS Code, aren't they both using the Anaconda distribution?
EDIT:
I tried using Sublime text editor and got the same error. When using the Anaconda Prompt and running 'pip list' I get a list of all packages including spacy, but when running the code I get an error "No module named 'spacy'"

Usually, this mistake happens if you didn't select the appropriate environment. To switch to anaconda environment in VSCode. Use the "Python: Select Interpreter" command from the Command Palette (Ctrl+Shift+P) then Select the specific environment. You can find more detail in VSCode documentation

Related

How can I import a conda library into a seperate Spyder distribution?

I have recently installed Spyder 2 on my MacBook. Then, since I couldn't import the sklearn library, i decided to also download Anaconda from their Website. Now, Anaconda comes with a Spyder environment itself, on which I can import sklearn, however the IDE is laggy. Is there a way to use the conda sklearn library on my separately downloaded Spyder (which runs much smoother)? Both Spyders are running Python 3.9.5 and I'm using MacOs 10.15.7
I tried setting a path via the PYTHONPATH Manager, but Spyder 2 forbids setting a path to 'site-packages' and after copying sklearn into another folder and setting a path there, import failed:
ModuleNotFoundError: No module named 'joblib'
I also installed sklearn via pip on the terminal, but I run into the same kind of problems if I try to import sklearn in my seperate Spyder 2('no module named sklearn found' or I can't set a path there or some module is missing).
And if I try to run
pip install scikit-learn
in the IPython console directly, I get
/Applications/Spyder 2.app/Contents/MacOS/python: No module named pip
while if I try
conda install scikit-learn
in the IPython console I get
ValueError: The python kernel does not appear to be a conda environment. Please use ``%pip install`` instead.
So I seem to be running in circles...
conda and pip are executables and not to be run from the IPython console, but from a command shell.
Don't mix conda and pip installations if you don't have to. Only install scikit-learn with conda.
Before you can use any installation, you have to activate the base environment first with conda activate.
There is no shame in reading the documentation first, e.g. https://docs.anaconda.com/anaconda/user-guide/getting-started/

How can you connect a jupyter notebook to a pipenv virtual environment using vscode?

I'm working on a project and was trying to set up a jupyter notebook to have access to the virtual environment. I set up the virtual environment just using pipenv install pandas, and that created the pipfile and the pip lock file. I then imported pandas in the jupyter notebook and tried printing out the version number, and got a ModuleNotFoundError: No module named 'pandas' error, meaning that the notebook is not connected to the virtual environment. I'm editing jupyter notebooks in vscode, and was wondering how I could connect the notebook to the pipenv environment?
Regarding using a virtual environment in Jyputer in VS Code and checking the python modules in this environment, you could refer to the following:
Please make sure that the upper right corner of Jupyter is using the python environment you need.
Please use "pip show pandas" to check whether the module "pandas" has been installed in the current environment.
In addition, since the python environment used by Jupyter in VS Code can be different from the environment used by VS Code, please check the selection of the relevant environment, and check the python environment currently used when installing the module on the VS Code terminal. (python --version or pip --version)
Reference: Python environments and Jupyter in VS Code.

How to install selenium for Jupyter Notebook?

I have run the command pip install selenium, and it can execute from selenium import webdriver in terminal, but it cannot execute this line in Jupyter Notebook, which shows "ModuleNotFoundError: No module named 'selenium'". Besides, I also copied chromedriver.exec file into the path of my jupyter. Does anyone konw how to solve it?
If you are using the Anaconda navigator to run Jupyter then you need to run the pip install selenium command within the Anaconda prompt.
I believe that your Python environment in Jupyter differs from the Python environment that may be in your PATH.

Running code from Jupyter-Notebook fails but runs with no problems in Terminal

I've hit a snag using Jupyter notebook in Anaconda on a Mac. My setup is:
MacOS Catalina 10.15.3 (default shell zsh)
Anaconda-Navigator 1.9.12
Jupyter-client 5.3.4
Jupyter-console 6.1.0
Jupyter-core 4..1
Basically, I have some Python code to extract some text from an old Word document. The code uses the textract library (https://textract.readthedocs.io/en/latest/index.html). Unfortunaltey, this library is not included in Anaconda and it has some other dependencies, most notably in this case a library called antiword.
The steps I've taken so far:
Installed Mac Ports and used it to install several packages (e.g. poppler, antiword, unrtf, tesseract, swig). Installation appeared to be successful. The directories /opt/local and /opt/local/bin (which is where the above binaries are located) were added to $PATH with no problems.
Created an environment (myenv) in Anaconda using Python 3.8. Added various libraries including jupyter.
Opened a Terminal with myenv activated. Installed textract using:
% pip install textract
This all seemed to work fine and there were no error messages.
Now, if I open myenv with Python or with iPython from the Anaconda-Navigator app, a terminal window opens and I can run the following commands:
>>> import textract
>>> myText = textract.process('path-to-doc-file')
>>> print(myText)
The output from the last command, as expected, is all the text contained in the Word document. This supports the idea that all the necessary libraries and dependencies have been installed correctly.
If I open myenv environment using Jupyter Notebook from the Anaconda-Navigator app, a web page appears and I can create a new notebook. However, if I try to run exactly the same commands in a cell of the notebook, I get the following errors:
FileNotFoundError: [Errno 2] No such file or directory: 'antiword'
The command `antiword path-to-file.doc` failed because the executable `antiword` is not installed on your system. Please make sure the appropriate dependencies are installed before using textract
I was surprised because I thought the Python used in the Terminal and in the Jupyter Notebook were essentially the same. And yet, that doesn't seem to be the case in this situation.
Am I missing a crucial setup step?

Anaconda/Python/VSCode: vscode editor doesn't recognize installed packages

I'm trying to use VS Code for python in an Anaconda environment. I have (after some googling and travail) gotten it using my custom environment. However, pylint and the editor don't recognize the installed netifaces in the environment.
The environment (billh) is being loaded:
The errors from python are shown here:
Oddly, importing from ipython running in the built-in terminal does work:
This was newly installed today, via Anaconda. Here's the version info
It isn't that the extension can't find the netifaces package, it's that Pylint can't. Make sure that the Pylint you are having the extension run for you is installed into the same conda environment that you are running from (e.g. make sure you didn't set python.linting.pylintPath to something outside of your conda environment). Also make sure that ipython is from the same environment as well (e.g. you are using a conda environment and launched the terminal with the Python: Create Terminal command).

Categories