Unable to import package in virtual environment using anaconda - python

I have installed a package in my virtual environment. The package is also listed in the conda list but when I try to import the package it returns an Import Error.
I was following the tutorial here

i just followed the whole tutorial,
first after creating the structure (schema in the tutorial):
1 . in terminal open your env
2. run python -m pip install --user --upgrade setuptools wheel
3. python setup.py sdist
4. python setup.py install
then in terminal (virtual env activated )
run commands
import example_pkg
it will import it.

Related

Install pandas in debian10 on a different python version

I have a device with python3.7 preinstalled, in which i have installed also python3.9. I managed to change the version I am using of python3 and now the command "python3" followed by the .py file runs with python3.9.
The problem is I tried installing pandas with pip3 but it does not work (it didn't work even in the preinstalled python3.7), so I found that in debian you can install package, for example in this case pandas, using "sudo apt-get install python3-pandas" but this command keeps installing pandas in python3.7 and not in python3.9 even if now "python3" refers to python3.9.
Has anyone ever encountered this problem and has a solution?
python3.9 -m pip install pandas
Venv
You could use a virtual environment (venv) for installing dependencies.
This venv could be project specific or global.
Run python3 -m venv .venv in your project folder to create a .venv folder, which holds the venv configuration.
Run source .venv/bin/activate to activate the venv. This will link pip3 from your python 3.9 version to the pip command.
Now you can do pip install pandas to install the pandas dependency into the venv.
Conda
Another solution would be to use Anaconda or Miniconda
https://pandas.pydata.org/pandas-docs/stable/getting_started/install.html
conda create -n name_of_my_env python
This will create a minimal environment with only Python installed in
it.
To put your self inside this environment run:
source activate name_of_my_env
On Windows the command is:
activate name_of_my_env
The final step required is to install pandas. This can be done with
the following command:
conda install pandas

Installing cusignal on windows 10

I wanted to install the cusignal python package on windows and I was following the instructions on the following github link.
It says to run the following commands:
conda create --name cusignal-dev
conda activate cusignal-dev
conda install numpy numba scipy cudatoolkit pip
pip install cupy-cuda101
cd python
python setup.py install
But towards the last two lines, I have no idea what and where the setup.py file is located. There is also no such folder called python that is automatically created. Hence, I am unsure how I am supposed to install cusignal library on windows 10.
The instructions expect that you first clone the GitHub repository, which has such a python folder, then run those instructions, like
git clone https://github.com/rapidsai/cusignal.git
cd python
python setup.py install
Be sure your environment is activated when doing this.

Python 3 cannot find a module

I am unable to install a module called 'jieba' in Python 3 which is running in Jupyter Notebook 6.0.0. I keep getting ModuleNotFoundError: No module named 'jieba' after trying these methods:
1. import jieba
2. pip3 install jieba
Can anyone help? Thank you.
pip3 in the terminal is almost certainly installing your package into a different Python installation.
Rather than have you hunt around for the right installation, you can use Python and Jupyter themselves to ensure you are using the correct Python binary. This relies on three tricks:
You can execute the pip command-line tool as a module by running python -m pip .... This uses the pip module installed for the python command, so you don't have to verify what python installation the pip3 command is tied to.
You can get the path of the current Python interpreter with the sys.executable attribute.
You can execute shell commands from Jupyter notebooks by prefixing the shell command with !, and you can insert values generated with Python code with {expression}
You can combine these to run pip (to install packages or run other commands) against the current Python installation, from Jupyter itself, by adding this into a cell:
import sys
!{sys.executable} -m pip <pip command line options>
To install your jieba package, that makes:
import sys
!{sys.executable} -m pip install jieba
If you are using Anaconda, then you could also install the conda package for jieba; the package does not require any platform-specific dependencies or compilation, but it may be more convenient for you or necessary to install other packages that do have such requirements and have pre-compiled conda packages.
In that case, tell the conda command about your Python executable:
import sys
!conda install --yes --prefix {sys.prefix} <package name>

virtualenv activation doesn't work

I've created a virtual environment with:
$ virtualenv my_ven_test
then let's activate the environment with:
$ source my_ven_test/bin/activate
now let's install a package:
(my_ven_test) $ pip install mysql-connector==2.1.3
This last line does not take effect. In fact if I check:
(my_ven_test) $ pip freeze
I see no package installed (as well as the my_ven_test/lib/python/site-package directory doesn't contain the mysql-connector package)
Could you guide me in solving this issue?
Some notes:
python version: 2.7
virtualenv version: 15.1.0
Forget about virtualenv, use the brand new Pipenv which is recommended by Python.org
Pipenv automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile (more about this below) as you install/uninstall packages.
First install pipenv using:
$ pip install pipenv
Then, for installing project specific packages, first create your project folder and then install all necessary packages for your project like:
$ mkdir myproject
$ cd myproject
# install `requests` library
$ pipenv install requests
# install more libraries required for your project
$ pipenv install mysql-connector
$ pipenv install numpy
This will create two files, namely Pipfile and Pipfile.lock. You can find the list of all installed packages for the current project in the file Pipfile while Pipfile.lock has info on hashes like sha256 for all the installed packages and their dependencies.
Once you're done with the installation of all necessary packages for your project, then do:
$ pipenv shell
which will launch a subshell in virtual environment. (This does the similar job of source /your/virtualenv/activate)
Then you can start coding.. For example, you can first test whether installed packages are working fine by launching a Python shell and import the packages like below:
$ python
>>> import requests
# ....
To exit out of the (virtualenv) shell, simply do:
$ exit
Now, you're out of the virtual environment created by pipenv
Read more about it installing packages for your project # pipenv.kennethreitz.org
When you are within a venv you should use the following to install a package:
py -m pip install mysql-connector==2.1.3
the -m ensures the package is installed into your venv and not into your root python
Try installing the package without activating virtualenv:
# Install it
my_ven_test/bin/pip install mysql-connector==2.1.3
# Use grep to check if exists
my_ven_test/bin/pip list | grep mysql-connector
If that works, then try to activate virtualenv by running this code:
. my_ven_test/bin/activate
Try installing another package
pip install flake8
Afterwards, search for those two packages
pip list | grep mysql-connector
pip list | grep flake8
Let me know the result.

Can't import PIL after installing Pillow

I have python 3.x, and was told to install Pillow for image manipulation. After installing it with pip however, i'm unable to import PIL from the python interpreter. It just says ImportError: No module named 'PIL'. Running pip list in the command line shows that Pillow is indeed installed.
It ended up installing correctly after using easy_install instead of pip.
You might have used pip for a different python executable. To make sure that the correct pip command is used, run:
$ python3 -m pip install pillow
I don't really have the time or the means to create an entire virtual machine just for the purposes of testing this one problem.
virtualenv is not a virtual machine. All you need to create a virtualenv from scratch:
$ python3 -m pip install --user virtualenv # install package
$ python3 -m virtualenv venv # create virtualenv named 'venv'
Then to activate the created virtualenv on POSIX system:
$ source venv/bin/activate # activate on POSIX
Or on Windows:
C:\> venv\Scripts\activate
Now, python, pip commands refers to the virtualenv. To deactivate virtualenv, run:
$ deactivate

Categories

Resources