Cannot connect to ipykernel Python 3 in Jupyter anymore - python-3.x

I am trying to learn more in Python. However, the fundamentals in terms of the software itself are hard to grasp for me. Let's say things got messy: I could not access all imports easily - so I decided to uninstall all Python related software to start over:
Uninstall Python, Jupyter and Anaconda
Re-install Anaconda and Python.
Installed it in C://Users/Mark/Anaconda
Added Python as PATH
But now I can not access my ipykernels anymore. It can not connect once I want to open a script I made. It asks which kernel I want to start every time. Was not the case before. It tries to connect but then says: "Disconnected".
I tried to move the ipynb files to the new root folder and open them from Path but no luck.
I tried to use cd in the Anaconda Prompt to set the root folder to C://Users/Mark/Anaconda
What is holding me from working scripts again?
I tried to move the ipynb files to the new root folder and open them from Path but no luck.
I tried to use cd in the Anaconda Prompt to set the root folder to C://Users/Mark/Anaconda

Related

Jupyter notebook open from cmd at any location or any drive

Recently I uninstalled Python and Anaconda. And again reinstalled it. But now I am facing an issue. Previously I was able to open jupyter notebook at any location in any drive by opening Command prompt by typing cmd in the address bar but it's now showing the following error:
> Fatal error in launcher: Unable to create process using '"C:\Python310\python.exe" "C:\Python310\Scripts\jupyter.exe" notebook': The system cannot find the file specified.
How should I handle this now? Please help. I want Jupyter notebook to be open at a specific folder location in a specific drive by opening command prompt through address bar. But not working now.
Your issue is probably related to your Windows environment variables.
To get to the interface, type the following into a terminal window with admin privileges:
rundll32 sysdm.cpl,EditEnvironmentVariables
Under 'Path' make sure that your python installation folder is present, if not add it. For me, python is under 'C:\Anaconda' and jupyter under 'C:\Anaconda\Scripts', but that is for an Anaconda installation. For reference see https://docs.python.org/3/using/windows.html#setting-envvars.
To make sure that everything works, type:
> where python
> where jupyter
You should see the respective installation folders now.

How to change Jupyter lab Working Directory

I'm using the latest version of Jupyter lab on a Windows 10 64 bit machine inside Firefox. When I start the Jupyter lab (via cmd) the default working directory appears to be
c:/windows/system32
(i.e the folder tab on the left) which is a terrible place to be meddling with, at leat for me. I don't know why Jupyter lab starts there. I've installed Jupyter lab using cmd with administrator privileges. Is that why? Should I have it installed just for a user? (My machine has only one user;me).
I tried changing the directory by using
jupyter notebook --generate-config
And inserted a path to my documents folder (in the path for kernels and notebooks line) , edited out the commentary ("#"). And when I restarted Jupyter lab nothing has changed. It shows the
c:/Windows/system32
as default directory which has so many windows folders and is too messy to deal with.
Any solutions will be greatly appreciated. Thanks in advance:)
A late answer to one of the most common Jupyter Notebook or Jupyter Lab questions most new users have about the file/folder structure when they first begin using Jupyter.
For those Windows users who may still be using that old school Windows Command Prompt - which is actually being phased out of Windows, the Windows Command Prompt will normally start-up and display the location where the Windows Command Prompt application is located on the Windows PC:
c:/Windows/system32
In a nutshell, the OP opened the Windows Command prompt, and then the OP typed the following code to start Jupyter Notebook:
Jupyter notebook
But when the OP created / saved the new Notebook file, it was saved to the Windows / system 32 folder - definitely not a location where anyone wants to save their files.
To avoid saving Jupyter Notebook files in the wrong place, the Command Prompt must be started in a different folder or be moved to another directory. One way to move the Command Prompt is by using an old dos command, Change Directory. Just type cd for Change Directory.
Best advice, create a new folder on the C drive and call it Jupyter_Files. Then use old dos, Change Director command, navigate from the location where the command prompt opens and move to that new folder. Do the following to navigate to the new folder.
cd..
cd..
cd Jupyter_Files
Now the Command Prompt is in the location where the Notebook files want to be saved. At the Command Prompt, type: Jupyter notebook. This will create and save those notebook files in this new folder.

How to simply compile a big python program with huge number of files, for linux, mac and windows [duplicate]

I'm trying to convert a fairly simple Python program to an executable and couldn't find what I was looking for, so I have a few questions (I'm running Python 3.6):
The methods of doing this that I have found so far are as follows
downloading an old version of Python and using pyinstaller/py2exe
setting up a virtual environment in Python 3.6 that will allow me to do 1.
downloading a Python to C++ converter and using that.
Here is what I've tried/what problems I've run into.
I installed pyinstaller before the required download before it (pypi-something) so it did not work. After downloading the prerequisite file, pyinstaller still does not recognize it.
If I'm setting up a virtualenv in Python 2.7, do I actually need to have Python 2.7 installed?
similarly, the only python to C++ converters I see work only up until Python 3.5 - do I need to download and use this version if attempting this?
Steps to convert .py to .exe in Python 3.6
Install Python 3.6.
Install cx_Freeze, (open your command prompt and type pip install cx_Freeze.
Install idna, (open your command prompt and type pip install idna.
Write a .py program named myfirstprog.py.
Create a new python file named setup.py on the current directory of your script.
In the setup.py file, copy the code below and save it.
With shift pressed right click on the same directory, so you are able to open a command prompt window.
In the prompt, type python setup.py build
If your script is error free, then there will be no problem on creating application.
Check the newly created folder build. It has another folder in it. Within that folder you can find your application. Run it. Make yourself happy.
See the original script in my blog.
setup.py:
from cx_Freeze import setup, Executable
base = None
executables = [Executable("myfirstprog.py", base=base)]
packages = ["idna"]
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "<any name>",
options = options,
version = "<any number>",
description = '<any description>',
executables = executables
)
EDIT:
be sure that instead of myfirstprog.py you should put your .pyextension file name as created in step 4;
you should include each imported package in your .py into packages list (ex: packages = ["idna", "os","sys"])
any name, any number, any description in setup.py file should not remain the same, you should change it accordingly (ex:name = "<first_ever>", version = "0.11", description = '' )
the imported packages must be installed before you start step 8.
Python 3.6 is supported by PyInstaller.
Open a cmd window in your Python folder (open a command window and use cd or while holding shift, right click it on Windows Explorer and choose 'Open command window here'). Then just enter
pip install pyinstaller
And that's it.
The simplest way to use it is by entering on your command prompt
pyinstaller file_name.py
For more details on how to use it, take a look at this question.
There is an open source project called auto-py-to-exe on GitHub. Actually it also just uses PyInstaller internally but since it is has a simple GUI that controls PyInstaller it may be a comfortable alternative. It can also output a standalone file in contrast to other solutions. They also provide a video showing how to set it up.
GUI:
Output:
Alternatively use pyinstaller directly:
pip install pyinstaller
pyinstaller filename
I can't tell you what's best, but a tool I have used with success in the past was cx_Freeze. They recently updated (on Jan. 7, '17) to version 5.0.1 and it supports Python 3.6.
Here's the pypi
https://pypi.python.org/pypi/cx_Freeze
The documentation shows that there is more than one way to do it, depending on your needs.
http://cx-freeze.readthedocs.io/en/latest/overview.html
I have not tried it out yet, so I'm going to point to a post where the simple way of doing it was discussed. Some things may or may not have changed though.
How do I use cx_freeze?
Now you can convert it by using PyInstaller. It works with even Python 3.
Steps:
Fire up your PC
Open command prompt
Enter command pip install pyinstaller
When it is installed, use the command 'cd' to go to the working directory.
Run command pyinstaller <filename>
I've been using Nuitka and PyInstaller with my package, PySimpleGUI.
Nuitka
There were issues getting tkinter to compile with Nuikta. One of the project contributors developed a script that fixed the problem.
If you're not using tkinter it may "just work" for you. If you are using tkinter say so and I'll try to get the script and instructions published.
PyInstaller
I'm running 3.6 and PyInstaller is working great!
The command I use to create my exe file is:
pyinstaller -wF myfile.py
The -wF will create a single EXE file. Because all of my programs have a GUI and I do not want to command window to show, the -w option will hide the command window.
This is as close to getting what looks like a Winforms program to run that was written in Python.
[Update 20-Jul-2019]
There is PySimpleGUI GUI based solution that uses PyInstaller. It uses PySimpleGUI. It's called pysimplegui-exemaker and can be pip installed.
pip install PySimpleGUI-exemaker
To run it after installing:
python -m pysimplegui-exemaker.pysimplegui-exemaker
The best and easiest way is auto-py-to-exe for sure, and I have given all the steps and red flags below which will take you just 5 mins to get a final .exe file as you don't have to learn anything to use it.
1.) It may not work for python 3.9 on some devices I guess.
2.) While installing python, if you had selected 'add python 3.x to path', open command prompt from start menu and you will have to type pip install auto-py-to-exe to install it. You will have to press enter on command prompt to get the result of the line that you are typing.
3.) Once it is installed, on command prompt itself, you can simply type just auto-py-to-exe to open it. It will open a new window. It may take up to a minute the first time. Also, closing command prompt will close auto-py-to-exe also so don't close it till you have your .exe file ready.
4.) There will be buttons for everything you need to make a .exe file and the screenshot of it is shared below. Also, for the icon, you need a .ico file instead of an image so to convert it, you can use https://convertio.co/
5.) If your script uses external files, you can add them through auto-py-to-exe and in the script, you will have to do some changes to their path. First, you have to write import sys if not written already, second, you have to make a variable for eg, location=getattr(sys,"_MEIPASS",".")+"/", third, the location of example.png would be location+"/example.png" if it is not in any folder.
6.) If it is showing any error, it may probably be because of a module called setuptools not being at the latest version. To upgrade it to the latest version, on command prompt, you will have to write pip install --upgrade setuptools. Also, in the script, writing import setuptools may help. If the version of setuptools is more than 50.0.0 then everything should be fine.
7.) After all these steps, in auto-py-to-exe, when the conversion is complete, the .exe file will be in the folder that you would have chosen (by default, it is 'c:/users/name/output') or it would have been removed by your antivirus if you have one. Every antivirus has different methods to restore a file so just experiment if you don't know.
Here is how the simple GUI of auto-py-to-exe can be used to make a .exe file.
PyOxidizer can be an option here. It's pretty popular with 3.3k stars on Github. Its documentation says
PyOxidizer is capable of producing a single file executable - with a copy of Python and all its dependencies statically linked and all resources (like .pyc files) embedded in the executable. You can copy a single executable file to another machine and run a Python application contained within. It just works.
While I'm not sure if it is capable of producing .exe file PyOxidizer definitely helps with packaging and distribution.

Anaconda Prompt(Anaconda3) does not recognize files in a directory

I took to heart the message that support for anaconda2 was dropped. I installed anaconda3 and used 2to3 to convert my *.py codes. Everything seemed to work as expected. I have several 2 line bat codes that cd to a particular directory and then call python to execute a particular *.py code in that directory. By loading Anaconda Prompt(Anaconda3) and invoking the bat file I could run pythons in that directory as expected.
Then, a couple of days ago, I made such a run and got an error message saying the *.py file could not be found. I immediately looked in the directory and the python file was there as expected. I then did a dir command in Anaconda Prompt(Anaconda3) and found that almost no files in the directory were listed. This was happening on my windows 10 laptop.
On my desktop, everything continues to work as expected. Are there any suggestions as to what could cause such a problem and how to fix it.
Thanks,
Mack Elrod
Responding to the request for additional information, I have a bin directory that is in path. In bin\MackData.bat is
c:
cd %USERPROFILE%\Documents\Medev\MackData
copy sugarhist.png sugarhist.old.png
python MackData.py
When I open Anaconda Prompt(Anaconda3) and enter MackData I get
(base) C:\Users\Mack>MackData
(base) C:\Users\Mack>c:
(base) C:\Users\Mack>cd C:\Users\Mack\Documents\Medev\MackData
(base) C:\Users\Mack\Documents\Medev\MackData>copy sugarhist.png sugarhist.old.png
1 file(s) copied.
(base) C:\Users\Mack\Documents\Medev\MackData>python MackData.py
python: can't open file 'MackData.py': [Errno 2] No such file or directory
(base) C:\Users\Mack\Documents\Medev\MackData>
But MackData.py is a python file in the directory MackData.
This code worked on this laptop until a few days ago and continues to work on my desktop computer. I can, of course, publish MackData.py but that seems irrelevant. The point is that Anaconda can't find it.
Thanks,
Mack
I must submit a sincere apology to this community. I have determined my problem and Anaconda is not responsible in any way. I thank all of you for you concerns and comments.
For any who might be interested I will give a brief outline of what happened. First, I installed Anaconda3. Several days later my directory, MackData, was moved to a new location. I conjecture that happened by me inadvertently letting my hand drag on the touch pad. Then, not knowing the location had changed I updated my laptop from my desktop. My update program not finding MackData where expected recreated it and put only the most recently changed files from the desktop into the new MackData. New but in the correct location. Then of course running my script in the Anaconda3 prompt failed. Doing a dir in the Anaconda3 prompt showed only a few files were there. I then went to Windows File Explore and looked at MackData and saw all of the expected files. I used the quick access feature which unknown to me pointed to the old directory in its new location.
That is not intended to be an excuse but only as an explanation. Again, I do apologize to the community.
Mack Elrod
I'm pretty sure newer anaconda launchers users different environment paths, and not even windows ones.
I suggest either checking the environment path within the anaconda launcher and configurations or, a better solution in my opinion, run these scrips using the regular python engine, without using anaconda (simpler, more determinant and works within your standard environment)

Anaconda Breaks Python

Just installed Anaconda distribution and now any time I try to run python by double clicking a script, or executing it in the command prompt (I'm using windows 10) , it looks for libraries in the anaconda folder rather than my python folder, and then crashes. If I run via the command prompt, I'm able to see the error, which is:
File "C:\Users\bob\Anaconda3\lib\site-packages\pandas__init__.py",
line 19, in
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
I've uninstalled and re-installed Python and numpy multiple times, but it's getting installed in the default python folder, and since I installed the anaconda distribution, the python launcher always looks in the Anaconda folder. I have to run modules from IDLE or not at all.
Is there any way to get Anaconda to play nice with the standard python installation? I'd really like to be able to quickly and easily double click python scripts to run them.
Anaconda comes with pip , you should not need a separate installation. If you need a library not available in anaconda then you would use pip. Both will store your downloaded packages in site-packages. In your scripts folder you should have [usually only one] reference to python. Generally I think anaconda is the preferred repository. I would consider removing " standard installation". If you need two for some reason make sure only one is in your environment variable path. Assuming you don't have the anaconda version in your path; go to the scripts folder ,in the cmd prompt and type
python myscript.py

Resources