How to use CPLEX with PYTHON on windows - python

Though I've already install Cplex, and could import cplex in Jupyter Notebook.
When I use "cplex.infinity", error arises and it says "module 'cplex' has no attribute 'infinity'"
I don't know the reason, hoping you could help me.

This is because virtual environments in Jupyter are different.If you don't tell your Jupiter kernel which environment it should use it won't use your environment from python terminal (even launching the jupyter notebook command with this virtual environment activated).
You can follow this guide.
Basically:
You have to install your virtual environment for Jupyter
You have to tell the kernel to use this environment in the top bar of your notebook
You will have then all the dependencies installed in your virtual environment available from Jupyter.
Cheers!

Related

Difference between Jupyter and Terminal in the same Kernel

I'm trying to import tensorflow as ts in my script. While everything is fine in a Notebook, when I try to recreate the same script in a .py file the import returns the following, popular message:
ModuleNotFoundError: No module named 'tensorflow'
Note that both Jupyter and terminal are using the same virtual environment. In Notebook it's picked as a Kernel and in terminal it's activated with conda.
I was convinced the whole Kernel/env idea was about preventing exactly that kind of situations. Although I'm new to python so maybe I'm missing something basic.
Also, this might have something to do with that I'm using an M1 and macos, so I have the tensorflow-macos installed. But please consider there might be other reasons.
Make sure your pyenv installation doesn't interfere with the conda environments. Pyenv can overrule which python installation is used even if the conda environment has picked another. In my case it was the reason. Jupyter Notebook wasn't affected by pyenv. There are two solutions:
Remove pyenv
Make sure it is clear to you how both pyenv and conda are affecting your setup and correct it. Some considerations and suggestions are mentioned in this question: Installing anaconda with pyenv, unable to configure virtual environment

Jupyter Notebook in VS Code is not working

I'm using Ubuntu, installed Vs Code and anaconda. I've also installed python extension in VS Code, which brings Jupyter Notebook extension. Now, whenever I'm trying to run code from the Jupyter extension, its throwing error as if it requires ipykernel.
Now, my conda 'base' environment has already ipykernel. Why cant Jupyter extension look for ipykernel from the current activated environment?
Is there any explicit settings to be made, to make VS Code Jupyter extension use ipykernel from the current active environment? Please provide it.
Click here to switch the environment of Jupyter Notebook.
Possible solution python -m ipykernel install --user --name=conda_env_name

Jupyter notebook doesn't see the environment's packages

I want to follow along a course about deeplearning. This course has a github repo, where all of the codes are available. We need to use anaconda, create an environment based on the yaml file, and run a jupyter notebook within that environment.
There's even a test notebook to check whether the installation and environment creating process was successful. I have created the environment as the description said, the installation of the packages seemed to be good.
For the first few command, I get the required outputs, but the notebook doesn't seem to start from the correct environment, it gives me
C:\Users\user\Anaconda3\Lib\site-packages\jupyter.py
instead of
C:\Users\paperspace\Anaconda3\envs\ztdl\lib\site-packages\jupyter.py
And it can't find the packages that I have just installed before. For example, when it has to include sklearn it gives me:
No module named 'sklearn.__check_build._check_build'
Installing them from command line with pip/conda solves this.
I have tried to install manually ipykernel, nb_conda, jupyter as other posts say. I ran jupyter notebook after I activated the conda environment from the anaconda prompt. But none of these helped me. As I open the kernel menu, I can see that the proper ztdl anaconda environment is the active one.
I also removed anaconda from the %PATH%, and added it again afterwards with c:\Users\user\Anaconda3\ .
I have no idea what should I do to run the notebook within this given anaconda environment, so I can access the installed packages. Thanks in advance, any help is appreciated.

When using Jupyter, I have to install python modules with Conda and not cmd prompt. Why?

I'm pretty new to programming so forgive my ignorance.
When installing certain python packages/modules in the cmd prompt I am able to import them fine when using Jupyter Notebook. But some modules Jupyter Notebook cannot import without installing via Conda first. Why is this?
The problem seems to be related to system and environment that you are using and not the programming :)
Since you are a beginner, let us understand the concepts first rather than solving the problem.
Python code is run on an interpreter that is installed on your machine.
Jupyter is a web application that takes code using a given language's interpreter. So Jupyter, on its own doesn't run your code. It uses the interpreter installed on a system(your local machine).
Conda is a package manager and also an environment manager. That means using conda, you can create a virtual environment on your machine and that virtual environment can have its own installation of an interpreter. This virtual environment can have its own copy of the packages/modules as well.
Now comes the best part: The jupyter notebook can be asked to use any of the interpreters including the ones installed on a virtual environment.
So most likely, you are running the jupyter notebook from an environment which doesn't have the required dependencies. So either run the jupyter notebook outside the environment or install the required packages in the environment where your jupyter notebook is running.
To know which environment is being used by your jupyter notebook, run below lines from the jupyter notebook cell:
import sys
sys.executable
If you don't get something like /usr/bin/python then the jupyter is running inside an environment. So you have to install all the packages/modules inside that environment only.

How to switch python env in Jupyter

My development environment is Anaconda3 with a virtual environment "OpenCV3".
I can run Jupyter in my main enviroment, but I don't know how to run jupyter in virtual environment "OpenCV3".
could you please show me how to do it?
The following options are available (there may be others available)
change conda environment from the terminal (using the appropriate command corresponding to your OS) and run jupyter notebook.
Install an ipython kernel for each environment as described in this answer. This will enable you to switch between environments directly in Jupyter, without having to manually switch among environments, from the Kernel>Change Kernel menu.

Categories

Resources