Error running exercises from ThinkStats on Jupyter Notebook - python

I am learning statistics and Python from this book called ThinkStats. It has instructions on how to download the code and solve the exercises. I did everything that was told but am still not able to run the code on jupyter notebook. I am not sure what I am missing. Can someone please help me? Here is a list of instructions in the book and the things I did till now.
"After you clone the repository or unzip the zip file, you should have a folder called ThinkStats2/code with a file called nsfg.py. If you run nsfg.py, it should read a data file, run some tests, and print a message like, “All tests passed.” If you get import errors, it probably means there are packages you need to install."
Till now I downloaded the zip file, unzipped it and got the file named nsfg.py. I opened the file in jupyter notebook, but I am getting an error called" cannot find module thinkstats2". This module is specific to the book and from what I understand, the module is in the Thinkstats2 directory. How do I make jupyter notebbok run the file in the ThinkStats 2 directory?

You can run nsfg.py using the Python interpreter in your terminal.
$ cd ~
$ git clone git#github.com:AllenDowney/ThinkStats2.git
$ cd ThinkStats2/code/
$ python3 nsfg.py
(13593, 244)
All tests passed.
Alternatively, you can launch Jupyter Notebook in a specific directory:
$ jupyter-notebook --notebook-dir=~/ThinkStats2/
If you navigate to http://localhost:8888/tree in your browser, you should see the contents of the ThinkStats2 directory. Then click "New" > "Python 3".
In the first cell block, enter cd code, and execute it. In the second cell block, enter %run nsfg.py. You should see the same message as with the Python interpreter.
Notice that you must be in the code/ directory to run the file because it uses relative file path references. This gets at a larger reason why I would not personally recommend learning Python with ThinkStats: it is statistically sound, but often it does not use the best Python practices and forms bad habit as a result. In other words, python ~/ThinkStats2/code/nsfg.py will raise a FileNotFoundError, which is easily avoidable.

Related

Running Python file uses the wrong directory? "No such file or directory" as a result

Brand new to Python, and I wrote a simple script to print some data from an Excel sheet using Pandas. I installed Pandas via Anaconda.
Weirdly when I run my .py file, ~1/4 times when I hit "Run Python File" in VSCode it gives me an error that it can't find the current .py file. The terminal shows:
... ~/Desktop/PSI Work/Python Data Work VSCode Project
$ Desktop/PSI Work/Python Data Work VSCode Project/dC:/Users/soup/anaconda3/python.exe "c:/Users/soup/ataWork.py"
bash: Desktop/PSI: No such file or directory
(base)
The filename is "dataWork.py", but for some reason it sometimes inserts the "d" in "dataWork" before the "C:" portion of the directory, breaking the path
.../dC:/Users/soup/anaconda3/python.exe
"c:/Users/soup/ataWork.py"
Running it again usually does the trick... but I'm a bit puzzled.

"python.exe can't find '__main__' module in ..." Error when trying to run python script opencv_blink_detect.py in virtual environment,

Apologies if there's missing information. I'm using python for medical research and am trying to run a program to count the number of blinks via video footage.
https://github.com/skvrahul/blink_detect
The script is located here
My virtual environment is set up here:
Package install instructions were followed from here
Terminal open via Anaconda
Attempt to run via readme instructions
Attempt to direct terminal to look at file path
As you can see, the error message is
(opencv-env) C:\Users\wmj>python C:\Users\wmj\Documents\Python Scripts\Biometrics\blink_detect-master.py -p -sp.dat
C:\Users\wmj\AppData\Local\Continuum\anaconda3\envs\opencv-env\python.exe: can't find 'main' module in 'C:\Users\wmj\Documents\Python'
The contents of C:\Users\wmj\Documents\Python is just Anaconda3-2018.12-Windows-x86_64
I have searched online and most solutions to this problem seem to be either solved by modifying the .py file or doing something with Pycharm
What is the solution to this? I want to be able to run the script as a demonstration to my supervisor
Thank you,
WW
Try first to cd to the exact location of the package and then run exactly as written in the readme instructions.
Python has to get the full path of the script you're trying to execute and the script itself has to get the sp.dat file as an argument.

/usr/bin/python: No module named barista.__main__; 'barista' is a package and cannot be directly executed

I am trying to drive github project: https://github.com/kjchavez/distributed-deep-q with Ubuntu16.04 python2.7.12. I have installed caffe and pycaffe correctly.
When i am trying to make a new caffemodel as with command below (given in readme)
python -m barista models/deepq/train_val.prototxt models/deepq/deepq.caffemodel --solver models/deepq/solver.prototxt
it gives me error
/usr/bin/python: No module named barista.__main __; 'barista' is a package and cannot be directly executed
I have read about same kind of errors in python2.6 with command "python -m" but im using python 2.7. Barista is a directory inside of the main project directory and it consist python files with __init __.py but not __main __.py. I have included to $PYTHONPATH in .bashrc file paths /caffe/python and /main_project_directory.
If someone have an idea where I fail I would be very happy. I think that problem is in python version but not sure about that. I can give extra information about my systems and setups in later posts.
The readme says you're supposed to use something like this to start the program:
python main.py models/deepq/train_val.prototxt models/deepq/deepq16.caffemodel
It seems that at some point there was a file [...]/barista/__main__.py which would allow to use python -m barista, but that file has been renamed to [...]/main.py, the description here probably just wasn't updated.

Python File Directory Can't be found

When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.
Thanks very much!
It seems that I have tried everything, and nothing is working. :(
I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.
Opening your terminal's respective shell, enter the following on your command line:
cd Desktop [to change directory to your desktop]
ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
python hello.py [to run your python file]
That should run it. Let me know if you run into errors.
In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space
Kidding!
You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command. Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory. Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.
I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:
First open your terminal, then type cd ~/Desktop. Now try running your python script.
EDIT:
Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)

CNTK tutorial:"Hands-On Lab: Image recognition with Convolutional Networks, Batch Normalization, and Residual Nets" python problems

I am trying to follow this tutorial:
https://github.com/Microsoft/CNTK/wiki/Hands-On-Labs-Image-Recognition
I am now at the point where Frank is saying:” Please execute the following two Python scripts which you will also find in the working directory:
wget -rc http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
tar xvf www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
python CifarConverter.py cifar-10-batches-py
I am using windows 10.
I assume that wget is a Linux “thing”. I have downloaded the file from http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
To the path of the CifarConverter.py script as i can not run wget from cmd or cygwin.
Next I am trying to run the tar command but get an error” No such file or directory”
I changed the command to tar xvf cifar-10-python.tar.gz and executed it from Cygwin.(I just made a fresh installation of cygwin 2.6.0) This extracts the data.
Next I am running the python command:” python CifarConverter.py cifar-10-batches-py” (from cygwin)
But I am getting an error from line 48!
I have tried change the line to: print ("error") but is only getting a new error in
import cPickle as cp
ImportError: No module named 'cPickle'
What shall i do to run the python script?
You are using Python 3.+ version. Try it with Python 2.7, and it should be ok.
It may be easier to follow this tutorial using the Jupyter Noteboks found here:
https://github.com/Microsoft/CNTK/blob/master/Tutorials/CNTK_201A_CIFAR-10_DataLoader.ipynb
https://github.com/Microsoft/CNTK/blob/master/Tutorials/CNTK_201B_CIFAR-10_ImageHandsOn.ipynb
These are installed with the CNTK and is very easy to click through. Just make sure to run the CNTP34PY.BAT file to activate the Python environment first, set default directory to the Tutorials folder and start Jupyter Notebook by typing "Jupyter Notebook" from the command prompt.
Previous answer works, if you want to keep using Python 3, change cPickle to Pickle and it should work...
Specifically:
Open Convert-CifarConverter.py in a text editor.
Find and Replace cPickle with Pickle (Should be two instances.. one
at the top where you import it, then one where it's being used in the
code).
Where it's being used in the code, may need to change some import
arguments if the code still doesn't run.
For example, the encoding:
pickle.load(f, encoding='latin1')

Categories