I'm getting started with virtual environments, and I'm having some sort of path issue.
First I create a virtual environment:
python -m venv conda-env
.\conda-env\Scripts\activate
(conda-env) shows up in the prompt.
Now I install a package:
pip install netcdf4
pip list
Package Version
---------- -------
cftime 1.1.3
netCDF4 1.5.3
numpy 1.19.0
pip 19.2.3
setuptools 41.2.0
Everything looks good. Now if i start a console and try to load the module, things go haywire:
jupyter console
In [1]: import netCDF4
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-9588a3d4fb24> in <module>
----> 1 import netCDF4
ModuleNotFoundError: No module named 'netCDF4'
If I run the console from the site-packages directory however, the following runs with no errors.
cd .\conda-env\Lib\site-packages\
jupyter console
import netCDF4
edit1:
If I install jupyter in my virtual environment, that fixes the problem.
pip install jupyter
I don't get it. Do I need to do this for every virtual environment?
edit2:
If I use python directly, instead of a jupyter console I don't get this problem. The code below works where test.py has the line import netCDF4.
python -m venv venv
./venv/Scripts/activate
pip install netCDF4
python test.py
edit 3
On a mac, pip install jupyter doesn't fix the problem. I'm very confused.
I set the PYTHONPATH environment variable in the virtual environment to include the virtual environment site-packages path, for ex.
# Setting the PYTHONPATH
export PYTHONPATH=/Users/leo/dev/lib/python3.9/site-packages
This should pick up the packages you installed in your virtual environment with pip install.
Related
I made a virtual env with:
$ python3 -m venv /home/jeanpat/Devel/icevision
Different modules were installed (pycocotools ...) with pip.
From a virtual environment activated as follow:
source /home/jeanpat/Devel/icevision/bin/activate
The modules can be imported from an ipython console with no error. When the modules are imported from a jupyter notebook run from the same env (icevision), the immport fails. from a notebook cell,I can run :
!pip freeze|grep pycocotools
and get the modules list:
pycocotools==2.0.4
I have:
(icevision) jeanpat:~/ $ jupyter --paths [11:06:36]
config:
/home/jeanpat/.jupyter
/home/jeanpat/Devel/icevision/etc/jupyter
/usr/local/etc/jupyter
/etc/jupyter
data:
/home/jeanpat/.local/share/jupyter
/home/jeanpat/Devel/icevision/share/jupyter
/usr/local/share/jupyter
/usr/share/jupyter
runtime:
/home/jeanpat/.local/share/jupyter/runtime
The problem is similar to, i try this:
$ ipython kernel install --name "icevision" --user
but it didn't resolve the problem.
Thanks for your suggestions.
I had to use the command to solve the issue in the answer by Peter S. Magnusson:
python -m ipykernel install --user --name="myenv" --display-name="My project (myenv)"
I have installed tidy by "conda install -c conda-forge tidy-html5" and then installed pytidylib
by "conda install -c hargup/label/pypi pytidylib".
The installation is fine. But when I do "import tidylib" I got the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-a3a284945f3a> in <module>
----> 1 import tidylib
~\anaconda3\lib\site-packages\tidylib\__init__.py in <module>
23 import re
24 import platform
---> 25 from sink import create_sink, destroy_sink
26
27 __all__ = ['tidy_document', 'tidy_fragment', 'release_tidy_doc']
ModuleNotFoundError: No module named 'sink'
I can't find any solution to the issue above. The internet is showing a lot on "
Error running kitchen_sink.py after installing kivyMD" which is not related. Has anyone managed to make tidylib work in window's environment? I'm running python on Juptyer.
Try running the command conda install sink.
If that does not work try creating a virtual environment using conda create -n yourenvname python=x.x anaconda then activate environment by conda activate yourenvname Install additional Python packages to a virtual environment by conda install -n yourenvname [package] to deactivate environment conda deactivate.Else try using pip install to install the modules outside anaconda.
First try these outside Jupyter to check whether everything is fine then in Jupyter.
I have already installed the Anaconda software based on Python 2.7, and the Python 3.4 kernel has also been configured.
Using pip3 install xxx, I can install some packages for Python 3 environment. In ipython3, they can be imported well. However, in Python 3 kernel of jupyter notebook, those packages can't be successfully imported.
# Python 3 environment inside the jupyter notebook
import numpy as np
> No module named 'numpy'
My kernel path:
Available kernels:
ir /Users/HYF/Library/Jupyter/kernels/ir
javascript /Users/HYF/Library/Jupyter/kernels/javascript
python2 /Users/HYF/anaconda/share/jupyter/kernels/python2
python3 /usr/local/share/jupyter/kernels/python3
The Python3 environment in jupyter kernel shows like this:
import sys
sys.executable
>'/Users/HYF/anaconda/envs/py35/bin/python'
I thought the problem is that the python3 package path is not loaded in jupyter notebook. How to fix this issue?
Try by installing directly within Jupyter using the following command in a Jupyter cell:
import sys
!{sys.executable} -m pip install your_package_name
You need to activate your python3 environment prior to installing packages:
On the command line: source activate python3_environmane_name (or the name of your python3 environment.
Then you either conda install package_name, or, if not available via conda, pip install package_name, or pip3 install package_name.
Using pip3 in the python2 environment will not magically install anything in another env.
Note: to return to the default env, on command line: source deactivate
I have installed a created a virtualenv machinelearn and installed a few python modules (pandas, scipy and sklearn) in that environment.
When I run jupyter notebook, I can import pandas and scipy in my notebooks - however, when I try to import sklearn, I get the following error message:
import sklearn
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-8fd979e02004> in <module>()
----> 1 import sklearn
ImportError: No module named 'sklearn'
I am able to import all modules, at the command line - so I know they have been successfully installed:
(machinelearn) me#yourbox:~/path/to/machinelearn$ python -c "import pandas, scipy, sklearn"
(machinelearn) me#yourbox:~/path/to/machinelearn$
How can I import sklearn in my jupyter notebook running in a virtualenv?
You probably have not installed jupyter / IPython in your virtualenv. Try the following:
python -c "import IPython"
and check that the jupyter command found in your $PATH is the one from the bin folder of your venv:
which jupyter
For windows users in a powershell console, you can use the following to check that the jupyter command in your $env:Path is the one from the Scripts folder of you venv:
get-command jupyter
Edit: if this is the problem, just run python -m pip install jupyter in your venv.
Edit 2: actually you might also need:
python -m ipykernel install --user --name=my-virtualenv-name
and then switch the kernel named "my-virtualenv-name" in the jupyter user interface.
Edit 3: maybe the --user flag in the last command is a bad idea:
python -m ipykernel install --name=my-virtualenv-name
Another approach to take is to have one global jupyter installation, but to point to different kernels to run as the backend.
That approach is outlined here in their docs:
http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs
Copying below in case the link breaks:
You can use a virtualenv for your IPython notebook. Follow the following steps:
Install the ipython kernel module into your virtualenv
workon my-virtualenv-name # activate your virtualenv, if you haven't already
pip install ipykernel
Now run the kernel "self-install" script:
python -m ipykernel install --user --name=my-virtualenv-name
Replacing the --name parameter as appropriate.
You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.
To use Jupyter notebook with virtual environment (using virtualenvwrapper) plus packages installed in that environment, follow steps below:
create a virtual environment
mkvirtualenv --no-site-packages --python=/your/python/path your_env_name
Activate the virtual environment
workon your_env_name
Install Jupyter and other packages
pip install jupyter, numpy
Add a new kernel to your Jupyter config
ipython kernel install --user --name=your_env_name
Done. You may now use Jupyter notebook under the virtual environment.
jupyter-notebook
Disclaimer: the question has been answered but is hidden in one of the replies. I googled and took sometime to find the right answer. So I just summarize it so someone having the same issue can easily follow.
Assuming that jupyter is installed on your machine, not on the virtual environtment.
Using a virtual environment with Jupyter notebook
VENV_NAME = "YOUR VIRTUAL ENV NAME"
1) virtualenv VENV_NAME
2) source venv/bin/activate
3) Add this package if not present: pip3 install ipykernel
4) Then execute this command: ipython kernel install --user --name=VENV_NAME
5) Now open up the Jupyter Notebook and in change kernel select VENV_NAME
6) To install a new package perform pip3 install <PACKAGE NAME> in your terminal and repeat step 4.
Hope it helps!
Solution without adding a new kernel globally!!
create a new virtual environment by
python3 -m virtualenv envname
Activate your enviroment and install jupyter in it by
pip install jupyter
One thing you have to make sure before installing jupyter is that you don't have following packages already installed in it.
ipykernel
ipython
ipython-genutils
ipywidgets
jupyter
jupyter-client
jupyter-console
jupyter-core
If you've previously installed them then first uninstall them by pip uninstall.
Install your desired packages in activated virtualenv and launch jupyter in it and voila!
Creation of virtualenv with python3 -m venv command
I had the same problem as yours.
In my case I had created the virtualenv with the command
python3 -m venv ./my_virtual_env --system-site-packages
The problem was I could not install jupyter inside the virtual environment as it was already in the system-site-package (when you try to install it, it tells you "Requirement already satisfied").
To install jupyter, (and in a first instance pip, that does not get installed neither in your virtual environment with this command) but still have access to system-site-package you can run :
python3 -m venv ./my_virtual_env
Activate you virtual environment, run pip3 install jupyter (and pip3 install pip) and then turn on the option include-system-site-packages in the file ./my_virtual_env/pyvenv.cfg.
After deactivation and reactivation of you environment, you will have access to system site-packages.
Creation of virtualenv with virtualenv command
Given this answer you can prevent the access to system site-packages by creating a file ./my_virtual_env/lib/python3.4/no-global-site-packages.txt,
and get the access back by removing it.
You can still install jupyter inside your virtual-environment if you have created your virtual env using:
python -m venv --system-site-packages path/to/my-venv
Simply do this:
activate-your-env
pip install -I jupyter
And you are now ready to go
jupyter notebook
I run the following line from Spyder (Anaconda3):
from plyfile import PlyData, PlyElement
and I get the following error message:
Traceback (most recent call last):
File "<ipython-input-269-2c796028388e>", line 1, in <module>
from plyfile import PlyData, PlyElement
ImportError: No module named 'plyfile'
Next I went to the Anaconda3 Scripts subdirectory and using the Windows Commander I wrote:
conda install plyfile
I received the following error message:
PackageNotFound: Package not found: "Package missing in current win 64 channels:
-plyfile
I made a search using the Google and I found the plyfile in the following address https://pypi.python.org/pypi/plyfile, but then I do not know what to do with it.
Could you please help me?
pip install plyfile, if something isn't in the default anaconda repository but still a pypi package you can pip install and conda will still track the package within your environment.
Try this. This worked for me.
sudo su
pip install plyfile
I got plyfile to work in the Windows 10 Pro environment using Spyder 3.1.3, with a Python 2.7 environment on Anaconda as follows:
Open the plyfile.py program file in Spyder #This program is in the plyfile-05 file in the pkgs subdirectory of anaconda3.
Run the plyfile.py program in spyder3 and run "import plyfile" from the IPython console. #If you use the "from plyfile import PlyData, PlyElement" statement you will get an error : TypeError: 'NoneType' object is not callable' when you run a program that calls PlyData or PlyElement.
Remove any import statement like "from plyfile import PlyData, PlyElement" from the program you want to use plyfile in. #Otherwise you will get the TypeError above when you run your program file.
Run your .py file now and the 3D figure should be generated.
First activate the conda environment where you want to install the plyfile package
source activate environment_name
Then use pip to install the plyfile package
pip install plyfile
After that you can check whether the package has been installed in the same conda environment by using
conda list
Find out executable used by Spyder or Jupyter
import sys
sys.executable
If its not using virtual environment executable
Setup ipykernal to use the virtual environment (devconda).
python -m ipykernel install --user --name=devconda
After this
sys.executable
/opt/anaconda/envs/devconda/bin/python
And the pip imports will work in Jupyter or Spyder