cannot activate virtualenv: No such file or directory - virtualenv

I have problem with activating virtualenv.
I'm working on the server and using SSH secure shell.
My final goal is to activate virtualenv and run the latest version of tensorflow
The following is the command lines:
jeonguyoang#vision6:~$ python3 -m venv tfenv
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
jeonguyoang#vision6:~$ source tfenv/bin/activate
-bash: tfenv/bin/activate: No such file or directory
jeonguyoang#vision6:~$ cd tfenv
jeonguyoang#vision6:~/tfenv$ ls
bin include lib lib64 pyvenv.cfg
jeonguyoang#vision6:~/tfenv$ cd bin
jeonguyoang#vision6:~/tfenv/bin$ ls
python python3
captured image of the commands
I think that there is no activate file.
Maybe re-installing virtualenv is the answer, but I cannot interrupt server settings..

Check if you have python 2 versions of pip and python (python-all & python-pip packages). Venv installs both v2 and v3 versions of python & pip (regardless of python version of venv).

Related

python2.7 in Raspbian Lite

I have installed Raspbian Lite OS in Raspberry Pi zero.
I found that Raspbian Lite comes with Python3 as default.
But I am gonna run some scripts that uses libraries that are Python2 Compatible.
So I tried to change the default Python version from Python3 to Python2 (Specifically Python2.7.18)
After so much searching and trying, instructions from [this page][1] made my job
Now if I try to check in command writing
python --version
It shows me that it is Python2.7.18
But the problem is I am not being able to install any packages using
sudo apt-get install <python-packagename>
It shows me Errors like
1.Package "python-pip" has no installation candidate (When I tried to install pip)
2. Package python-numpy has no installation candidate (when I tried to install numpy)
3. unable to locate package python-pyaudio (when I tried to install pyaudio)
I am searching but no solution.
Can anyone please help? I am frozen in a critical stage of my project .
Generally, for new raspbian/raspios os with python 3 by default, it is not suggested to replace the system python interpreter to python 2, it may break some system component's dependency.
Instead, you can create a python 2 virtual env, if you need to run python2 scripts.
sudo apt-get install python3-pip
pip install virtualenv
virtualenv -p /usr/bin/python2.7 venv
source venv/bin/activate
You can test the python version as following,
(venv) $ python --version

Installing Network-x

How do I download and install network-x for python version 3.4.0 on mac? Can someone give me a step by step guide on installing network-x? I tried to quick 'quick install' as suggested by github.
When I try to import it on python it gives me an import error saying no modules were found.
Ok so I have figured out an easier way:
(Answering my own question)
I first download the networkx-1.10.zip (md5) zip file from https://pypi.python.org/pypi/networkx. Unzipped it. Opened the terminal and typed cd
Then, I dragged and dropped the unzipped folder (networkx-1.10) into the terminal. Hit Enter.
Then I proceeded with the following commands:
python3 --version
ls
umask
sudo python setup.py install
sudo python3 setup.py install
python3
It worked like a charm.
this has worked for me
sudo pip install networkx
This should work:
sudo pip3 install networkx
if you have annaconda, in your terminal or cmd
> % conda install -c anaconda networkx
> % conda install -c anaconda graphviz
> % conda install -c anaconda pydot
> % conda install -c pdrops pygraphviz
The other packages come in handy as well
The easiest way is using Anaconda, since networkX is installed by default. Just follow the instructions on their website (they have both a graphical and command line installer).

How do I add python3 kernel to jupyter (IPython)

My Jupyter notebooks installed with python 2 kernel. I do not understand why. I might have messed something up when I did the install. I already have python 3 installed. How can I add it to Jupyter?
Here's a screenshot of what the default Jupyter insalled with python3 -m install jupyter and opened in the browser with jupyter notebooklooks like:
This worked for me on Ubuntu 16.04:
python2 -m pip install ipykernel
python2 -m ipykernel install --user
python3 -m pip install ipykernel
python3 -m ipykernel install --user
Reference to the documentation:
Kernels for Python 2 and 3. Installing the IPython kernel - IPython Docs.
Make sure you have ipykernel installed and use ipython kernel install to drop the kernelspec in the right location for python2. Then ipython3 kernel install for Python3. Now you should be able to chose between the 2 kernels regardless of whether you use jupyter notebook, ipython notebook or ipython3 notebook (the later two are deprecated).
Note that if you want to install for a specific Python executable you can use the following trick:
path/to/python -m ipykernel install <options>
This works when using environments (venv,conda,...) and the <options> let you name your kernel (see --help). So you can do
conda create -n py36-test python=3.6
source activate py36-test
python -m ipykernel install --name py36-test
source deactivate
And now you get a kernel named py36-test in your dropdown menus, along the other ones.
See Using both Python 2.x and Python 3.x in IPython Notebook which has more recent information.
Got ipython notebook with Python2 (on Windows7)
upgrade to Jupyter with pip install -U jupyter
install Python3
install Jupyter again using pip3 install jupyter
install Python3 kernel using ipython3 kernelspec install-self
I finally got 2 working kernels.
for recent versions of jupyter/ipython: use jupyter kernelspec
Full doc: https://ipython.readthedocs.io/en/latest/install/kernel_install.html
list current kernels
$ jupyter kernelspec list
Available kernels:
python2 .../Jupyter/kernels/python2
python3 .../Jupyter/kernels/python3
In my case, the python3 kernel setup was broken because the py3.5 linked was no longer there, replaced by a py3.6
add/remove kernels
Remove:
$ jupyter kernelspec uninstall python3
Add a new one:
Using the Python you wish to add and pointing to the python which runs your jupiter:
$ /path/to/kernel/env/bin/python -m ipykernel install --prefix=/path/to/jupyter/env --name 'python-my-env'
See more examples in https://ipython.readthedocs.io/en/6.5.0/install/kernel_install.html#kernels-for-different-environments
List again:
$ jupyter kernelspec list
Available kernels:
python3 /usr/local/lib/python3.6/site-packages/ipykernel/resources
python2 /Users/stefano/Library/Jupyter/kernels/python2
Doc: https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs
Details
Kernels available are listed under the kernels folder in Jupyter DATA DIRECTORY (see http://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html for details).
For instance on macosx that would be /Users/YOURUSERNAME/Library/Jupyter/kernels/
the kernel is simply described by a kernel.json file, eg. for /Users/me/Library/Jupyter/kernels/python3/kernel.json
{
"argv": [
"/usr/local/opt/python3/bin/python3.5",
"-m",
"ipykernel",
"-f",
"{connection_file}"
],
"language": "python",
"display_name": "Python 3"
}
Rather then manipulating that by hand, you can use the kernelspec command (as above). It was previously available through ipython now through jupyter (http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments - https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs).
$ jupyter kernelspec help
Manage Jupyter kernel specifications.
Subcommands
-----------
Subcommands are launched as `jupyter kernelspec cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter kernelspec cmd -h`.
list
List installed kernel specifications.
install
Install a kernel specification directory.
uninstall
Alias for remove
remove
Remove one or more Jupyter kernelspecs by name.
install-self
[DEPRECATED] Install the IPython kernel spec directory for this Python.
To see all available configurables, use `--help-all`
Kernels for other languages
By the way, not strictly related to this question but there's a lot of other kernels available... https://github.com/jupyter/jupyter/wiki/Jupyter-kernels
Following command works for me, on macOS Sierra(10.12.3)
python3 -m pip install ipykernel
python3 -m ipykernel install --user
check it with
jupyter kernelspec list
Available kernels:
python3 /Users/admin/Library/Jupyter/kernels/python3
python2 /anaconda2/share/jupyter/kernels/python2
If you are using the anaconda distribution, this worked for me (on a macintosh):
$ conda create -n py3k python=3 anaconda
$ source activate py3k
$ ipython kernelspec install-self
Just a note for the last command:
(py3k)Monas-MacBook-Pro:cs799 mona$ ipython kernelspec install-self
[TerminalIPythonApp] WARNING | Subcommand `ipython kernelspec` is deprecated and will be removed in future versions.
[TerminalIPythonApp] WARNING | You likely want to use `jupyter kernelspec` in the future
[InstallNativeKernelSpec] WARNING | `jupyter kernelspec install-self` is DEPRECATED as of 4.0. You probably want `ipython kernel install` to install the IPython kernelspec.
[InstallNativeKernelSpec] Installed kernelspec python3 in /usr/local/share/jupyter/kernels/python3
(py3k)Monas-MacBook-Pro:cs799 mona$ ipython kernel install
Installed kernelspec python3 in /usr/local/share/jupyter/kernels/python3
As tested in OSX Yosemite with the above steps and entering jupter notebook and creating a new notebook in the browser you will see the following screenshot:
I successfully installed python3 kernel on macOS El Capitan (ipython version: 4.1.0) with following commands.
python3 -m pip install ipykernel
python3 -m ipykernel install --user
You can see all installed kernels with jupyter kernelspec list.
More info is available here
open terminal(or cmd for window), then run following commands:
(On window, drop "source" in the second line.)
conda create -n py35 python=3.5
source activate py35
conda install notebook ipykernel
ipython kernel install --user --name=python3.5
I tried some method but It doesnt work, then I found this way.
It worked with me. Hoping it can help.
I had Python 2.7 and wanted to be able to switch to Python 3 inside of Jupyter.
These steps worked for me on a Windows Anaconda Command Prompt:
conda update conda
conda create -n py33 python=3.3 anaconda
activate py33
ipython kernelspec install-self
deactivate
Now after opening ipython notebook with the usual command for Python2.7, Python3.3 is also available when creating a new notebook.
Shortest and best solution from my side
conda create --name py3-ml python=3.6
conda install ipykernel
source activate py3-ml # or conda activate py3-ml
python -m ipykernel install --user --name py3-ml
sudo apt-get install python3-pip python3-dev
pip3 install -U jupyter
For the current Python Launcher
If you have Py3 installed but default to py2
py -3 -m pip install ipykernel
py -3 -m ipykernel install --user
If you have Py2 installed but default to py3
py -2 -m pip install ipykernel
py -2 -m ipykernel install --user
On Ubuntu 14.04 I had to use a combination of previous answers.
First, install pip3
apt-get install python-pip3
Then with pip3 install jupyter
pip3 install jupyter
Then using ipython3 install the kernel
ipython3 kernel install
None of the other answers were working for me immediately on ElementaryOS Freya (based on Ubuntu 14.04); I was getting the
[TerminalIPythonApp] WARNING | File not found: 'kernelspec'
error that quickbug described under Matt's answer. I had to first do:
sudo apt-get install pip3, then
sudo pip3 install ipython[all]
At that point you can then run the commands that Matt suggested; namely: ipython kernelspec install-self and ipython3 kernelspec install-self
Now when I launch ipython notebook and then open a notebook, I am able to select the Python 3 kernel from the Kernel menu.
I managed to install a Python3 kernel besides the Python2. Here is the way I did it:
open a new notebook in Jupyter
copy and run the two cells here: Enable-Python-3-kernel
The latest working link can be found here.
The actual code is:
! mkdir -p ~/.ipython/kernels/python3
%%file ~/.ipython/kernels/python3/kernel.json
{
"display_name": "IPython (Python 3)",
"language": "python",
"argv": [
"python3",
"-c", "from IPython.kernel.zmq.kernelapp import main; main()",
"-f", "{connection_file}"
],
"codemirror_mode": {
"version": 2,
"name": "ipython"
}
}
Adding kernel means you want to use Jupyter Notebook with versions of python which are not showing up in the list.
Simple approach- Start notebook with required python version, suppose I have python3.7 installed then use below command from terminal (cmd) to run notebook:
python3.7 -m notebook
Sometimes instead of python3.7 it's install with alias of py, py3.7, python.
To add specific python into the jupyter kernel, first check the paths of the available python or python3 using the following command
$ where python3
Let's say you have '/usr/local/bin/python3' as one of the paths. To create a kernel for this version of python with name 'name_to_new_kernel' which will showup in jupyter,
$ /usr/local/bin/python3 -m pip install ipykernel
$ /usr/local/bin/python3 -m ipykernel install --user --name name_to_new_kernel
To check if the new kernel with name 'name_to_new_kernel' added to jupyter run the following command
jupyter kernelspec list
The solution is well documented in the official docs: https://ipython.readthedocs.org/en/latest/install/kernel_install.html
I tried the first approach. Since I already had ipykernel installed, simply running python3 -m ipykernel install --user solved the problem.
I was facing the same issue. Whenever I would start jupyter notebook using the following command, I would not see an option to create a new Python 3 Notebook.
I solved the issue by the running the following command.
pip install notebook
After that I started notebook with the same command as before
jupyter notebook
I was able to see the my home directory and create new python3 notebooks.
This answer explains how to create a Python 3, Jupyter 1, and ipykernel 5 workflow with Poetry dependency management. Poetry makes creating a virtual environment for Jupyter notebooks easy. I strongly recommend against running python3 commands. Python workflows that install global dependencies set you up for dependency hell.
Here's a summary of the clean, reliable Poetry workflow:
Install the dependencies with poetry add pandas jupyter ipykernel
Open a shell within the virtual environment with poetry shell
Open the Jupyter notebook with access to all the virtual environment dependencies with jupyter notebook
There are clean Conda workflows as well. Watch out for a lot of the answers in this thread - they'll set you down a path that'll cause a lot of pain & suffering.
INSTALLING MULTIPLE KERNELS TO A SINGLE VIRTUAL ENVIRONMENT (VENV)
Most (if not all) of these answers assume you are happy to install packages globally. This answer is for you if you:
use a *NIX machine
don't like installing packages globally
don't want to use anaconda <-> you're happy to run the jupyter server from the command line
want to have a sense of what/where the kernel installation 'is'.
(Note: this answer adds a python2 kernel to a python3-jupyter install, but it's conceptually easy to swap things around.)
Prerequisites
You're in the dir from which you'll run the jupyter server and save files
python2 is installed on your machine
python3 is installed on your machine
virtualenv is installed on your machine
Create a python3 venv and install jupyter
Create a fresh python3 venv: python3 -m venv .venv
Activate the venv: . .venv/bin/activate
Install jupyterlab: pip install jupyterlab. This will create locally all the essential infrastructure for running notebooks.
Note: by installing jupyterlab here, you also generate default 'kernel specs' (see below) in $PWD/.venv/share/jupyter/kernels/python3/. If you want to install and run jupyter elsewhere, and only use this venv for organizing all your kernels, then you only need: pip install ipykernel
You can now run jupyter lab with jupyter lab (and go to your browser to the url displayed in the console). So far, you'll only see one kernel option called 'Python 3'. (This name is determined by the display_name entry in your kernel.json file.)
Add a python2 kernel
Quit jupyter (or start another shell in the same dir): ctrl-c
Deactivate your python3 venv: deactivate
Create a new venv in the same dir for python2: virtualenv -p python2 .venv2
Activate your python2 venv: . .venv2/bin/activate
Install the ipykernel module: pip install ipykernel. This will also generate default kernel specs for this python2 venv in .venv2/share/jupyter/kernels/python2
Export these kernel specs to your python3 venv: python -m ipykernel install --prefix=$PWD/.venv. This basically just copies the dir $PWD/.venv2/share/jupyter/kernels/python2 to $PWD/.venv/share/jupyter/kernels/
Switch back to your python3 venv and/or rerun/re-examine your jupyter server: deactivate; . .venv/bin/activate; jupyter lab. If all went well, you'll see a Python 2 option in your list of kernels. You can test that they're running real python2/python3 interpreters by their handling of a simple print 'Hellow world' vs print('Hellow world') command.
Note: you don't need to create a separate venv for python2 if you're happy to install ipykernel and reference the python2-kernel specs from a global space, but I prefer having all of my dependencies in one local dir
TL;DR
Optionally install an R kernel. This is instructive to develop a sense of what a kernel 'is'.
From the same dir, install the R IRkernel package: R -e "install.packages('IRkernel',repos='https://cran.mtu.edu/')". (This will install to your standard R-packages location; for home-brewed-installed R on a Mac, this will look like /usr/local/Cellar/r/3.5.2_2/lib/R/library/IRkernel.)
The IRkernel package comes with a function to export its kernel specs, so run: R -e "IRkernel::installspec(prefix=paste(getwd(),'/.venv',sep=''))". If you now look in $PWD/.venv/share/jupyter/kernels/ you'll find an ir directory with kernel.json file that looks something like this:
{
"argv": ["/usr/local/Cellar/r/3.5.2_2/lib/R/bin/R", "--slave", "-e", "IRkernel::main()", "--args", "{connection_file}"],
"display_name": "R",
"language": "R"
}
In summary, a kernel just 'is' an invocation of a language-specific executable from a kernel.json file that jupyter looks for in the .../share/jupyter/kernels dir and lists in its interface; in this case, R is being called to run the function IRkernel::main(), which will send messages back and forth to the Jupiter server. Likewise, the python2 kernel just 'is' an invocation of the python2 interpreter with module ipykernel_launcher as seen in .venv/share/jupyter/kernels/python2/kernel.json, etc.
Here is a script if you want to run all of these instructions in one fell swoop.
Here's a Windows/non command line method I found, which worked for me:
Find the folder where the kernel files are stored (on my machine - C:\ProgramData\jupyter\kernels - note that ProgramData is a hidden folder), create a copy of the existing kernel's folder, change the name and edit the json file within to point to the new kernel's directory. In this json you can also edit the kernel name that is displayed in ipython (e.g. instead of just python 2 you can specify 2.7.9 if you need to further distinguish for some reason).
When you use conda managing your python envs, follow these two steps:
activate py3 (on Windows or source activate py3 on Linux)
conda install notebook ipykernel or just use conda install jupyter
I was getting same error with python-2. I wanted to run python-2 jupyter notebook session but by default I was getting python-3. So easiest work around is open Anaconda terminal for python-2 and type 'jupyter notebook' , it will launch jupyter-notebook session without any problem. Similary it could be tried with python-3
I am pretty sure all you have to do is run
pip3 install jupyter

package is installed via pip in wrong (src) directory instead of site packages

I'm installing this package into a virtualenv using virtualenvwrapper and pip with this command:
pip install -e git+git://github.com/mr-stateradio/django-exchange.git#egg=django_exchange-master
Interestingly the package is then placed into a src folder, and not into the site-packages folder which I would have expected. The package is placed into this folder:
<path-to-my-virtual-env>/testenv/src/django-exchange-master/exchange
Instead of this:
<path-to-my-virtual-env>/testenv/lib/python2.7/site-packages
I assume something is wrong with the pip install command I'm using or with the setup.py of the package.
The -e option tells pip to install packages in “editable” mode. If you remove the -e option, pip will install the package into <venv path>/lib/Python_version/site-packages. Don't forget to remove the packages inside <venv path>/src, because python looks for the packages inside <venv path>/src first.
pip supports installing from Git, Mercurial, Subversion and Bazaar, and detects the type of VCS using url prefixes: “git+”, “hg+”, “bzr+”, “svn+”.
e.g
$ pip install -e git+https://git.repo/some_pkg.git#egg=SomePackage # from git
$ pip install -e hg+https://hg.repo/some_pkg.git#egg=SomePackage # from mercurial
$ pip install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomePackage # from svn
$ pip install -e git+https://git.repo/some_pkg.git#feature#egg=SomePackage # from 'feature' branch
VCS projects can be installed in editable mode (using the –editable option) or not.
For editable installs, the clone location by default is <venv path>/src/SomeProject in virtual environments, and <cwd>/src/SomeProject for global installs. The –src option can be used to modify this location.
For non-editable installs, the project is built locally in a temp dir and then installed normally. `

How can I make a list of installed packages in a certain virtualenv?

You can cd to YOUR_ENV/lib/pythonxx/site-packages/ and have a look, but is there any convenient ways?
pip freeze list all the packages installed including the system environment's.
You can list only packages in the virtualenv by
pip freeze --local
or
pip list --local.
This option works irrespective of whether you have global site packages visible in the virtualenv.
Note that restricting the virtualenv to not use global site packages isn't the answer to the problem, because the question is on how to separate the two lists, not how to constrain our workflow to fit limitations of tools.
Credits to #gvalkov's comment here. Cf. also pip issue 85.
Calling pip command inside a virtualenv should list the packages visible/available in the isolated environment. Make sure to use a recent version of virtualenv that uses option --no-site-packages by default. This way the purpose of using virtualenv is to create a python environment without access to packages installed in system python.
Next, make sure you use pip command provided inside the virtualenv (YOUR_ENV/bin/pip). Or just activate the virtualenv (source YOUR_ENV/bin/activate) as a convenient way to call the proper commands for python interpreter or pip
~/Projects$ virtualenv --version
1.9.1
~/Projects$ virtualenv -p /usr/bin/python2.7 demoenv2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in demoenv2.7/bin/python2.7
Also creating executable in demoenv2.7/bin/python
Installing setuptools............................done.
Installing pip...............done.
~/Projects$ cd demoenv2.7/
~/Projects/demoenv2.7$ bin/pip freeze
wsgiref==0.1.2
~/Projects/demoenv2.7$ bin/pip install commandlineapp
Downloading/unpacking commandlineapp
Downloading CommandLineApp-3.0.7.tar.gz (142kB): 142kB downloaded
Running setup.py egg_info for package commandlineapp
Installing collected packages: commandlineapp
Running setup.py install for commandlineapp
Successfully installed commandlineapp
Cleaning up...
~/Projects/demoenv2.7$ bin/pip freeze
CommandLineApp==3.0.7
wsgiref==0.1.2
What's strange in my answer is that package 'wsgiref' is visible inside the virtualenv. Its from my system python. Currently I do not know why, but maybe it is different on your system.
In Python3
pip list
Empty venv is
Package Version
---------- -------
pip 19.2.3
setuptools 41.2.0
To create a new environment
python3 -m venv your_foldername_here
Activate
cd your_foldername_here
source bin/activate
Deactivate
deactivate
You can also stand in the folder and give the virtual environment a name/folder (python3 -m venv name_of_venv).
Venv is a subset of virtualenv that is shipped with Python after 3.3.
list out the installed packages in the virtualenv
step 1:
workon envname
step 2:
pip freeze
it will display the all installed packages and installed packages and versions
If you're still a bit confused about virtualenv you might not pick up how to combine the great tips from the answers by Ioannis and Sascha. I.e. this is the basic command you need:
/YOUR_ENV/bin/pip freeze --local
That can be easily used elsewhere. E.g. here is a convenient and complete answer, suited for getting all the local packages installed in all the environments you set up via virtualenvwrapper:
cd ${WORKON_HOME:-~/.virtualenvs}
for dir in *; do [ -d $dir ] && $dir/bin/pip freeze --local > /tmp/$dir.fl; done
more /tmp/*.fl
why don't you try pip list
Remember I'm using pip version 19.1 on python version 3.7.3
If you are using pip 19.0.3 and python 3.7.4. Then go for pip list command in your virtualenv. It will show all the installed packages with respective versions.
.venv/bin/pip freeze worked for me in bash.
In my case the flask version was only visible under so I had to go to
C:\Users\\AppData\Local\flask\venv\Scripts>pip freeze --local
Using python3 executable only, from:
Gitbash:
winpty my_venv_dir/bin/python -m pip freeze
Linux:
my_venv_dir/bin/python -m pip freeze