It must be very simple, but somehow I am missing something... Apologies for the long message but it's driving me crazy.
I'm reading thinkstat2, a free pdf about statistics for data science (beginner level).
It comes with exercises so I'm trying to follow the instructions and import the data. I'll describe the steps I'm following and give the pages, it's like a 2 minutes read.
Step 1a: Page 8 - "0.2 Using the code" - Get the code from author's github. I don't have a github account, so I dl the code as a zip on my hard drive. Then I extracted the content in a new folder.
Step 1b: Page 9 - Author suggests to get anaconda. From my understanding, anaconda contains many different app you can use through the "anaconda navigator". I have anaconda and will use Jupyter notebook for this.
Step 2: Page 9 - Author asks to run "nsfg.py" from his zip to make sure the reader has all the packages installed. Author says I should have a confirmation message. When I run it, there's a flash of command screen without any error or confirmation message. But if it's all about checking that the right package are installed correctly, I can just add "import x" in my code to add the missing package, so not a real issue here (or so I believed). I moved on.
Step 3: Pages 24 to 26 - "1.3 Importing the data" and "1.4 Dataframes" - This is where I'm stuck. After running "nsfg.py" (I mentioned in step 2), the author asks to "import nsfg", which is his own package created from "nsfg.py"? Maybe? So I try to import it and it's not recognized. Which probably comes from that "not a real issue" in step 2... I checked inside "nsfg.py" and it seems to be code to clean and read a .dct and .gz file.
Questions:
I don't understand what I'm supposed to do to make "nsfg.py" run properly.
I don't understand how my jupyter notebook would understand the "import nsfg" since those lies on my hard drive. The author does not add any line regarding importing anything from a local drive, or from a url to his github (not in the code of nsfg.py as well, except if I missed it).
I thought about just copy pasting the code of "nsfg.py" and running it, but I believe it would not work since the .dct and .gz file mentioned are saved locally as well. So I don't get how the code pasted from nsfg.py I run on jupyter notebook would make a link with those local .dct and .gz files. My python is not advanced enough to tinker that manually...
Help :( (any alternative solution is welcome)
Two ways to solve this, both worked for me:
Firstly, to replicate what the extracts of the book say, all assuming you are using linux or mac:
Hit git clone https://github.com/AllenDowney/ThinkStats2.git in your terminal in whatever folder you want to wok in
Then cd ThinkStats2/code.
Then python nsfg.py
Then hit python again to give you an interactive shell which is what the author of the book appears to be using - note the >>> before each command which indicates that it isn't in a Jupyter notebook.
To get this to work in a Jupyter notebook:
Hit git clone https://github.com/AllenDowney/ThinkStats2.git in your terminal in whatever folder you want to wok in
In your jupyter notebook interface, create a new notebook in the same directory as the ThinkStats2/code folder.
In the notebook hit import nsfg.
I think you are probably getting errors because you are not running your notebook from the same directory as the nsfg.py file which means that python can't find the script and so can't import it.
Related
I just got two days worth of work vanished because jupyter notebook was not able to save (_xsrf argument missing).
I browse the internet for solutions:
looking at the running directory in jupyter
looking at the saved version in jupyter (there are none in my case)
I am starting to look at the chrome logs to at least see everything I typed but they are binary. Would you know where I could find logs of everything that was typed on chrome ? Or any other solution to retrieve that code ? You would be my savior !
I had the same issue and was able to recover all of my code from the 'history.sqlite' file in the following directory: ~/.ipython/profile_default/
(on Windows it's C:/Users/USERNAME/.ipython/profile_default/history.sqlite).
This is a file that saves all commands that were issued to the IPython kernel. At the end of the file (opened in a text editor) I found my most recent commands from the Jupyter notebook. This will only work if you did actually run your code though, not if it was just written.
Some more information is on the website where I found this solution: https://medium.com/flatiron-engineering/recovering-from-a-jupyter-disaster-27401677aeeb
I recently installed the Python VSCode extension, and when I did it popped up with a handy tutorial page.
However, after closing it, I cannot for the life of me figure out how to get this page back.
I've tried reinstalling the extension, checking through all of the commands, modifying settings, nothing seems to work.
Checking through the repo for the python extension, there seems to be a onWalkthrough:pythonWelcome that creates this page. However, I have absolutely no idea how to manually invoke this again.
Does anyone know how to do this? I know it's not strictly necessary, but I just assumed it would be easy and was surprised and stubbourn when it wasn't.
After asking the devs directly, it seems that it's extremely simple!
Get Started: Open Walkthrough... in the command palette will open a menu of available walkthroughs from extensions you've installed, including the Python one I was trying to find.
It has been removed, you can refer to these page for more details:
Should we not open the start page if a user already has a folder open?
Python Start Page
Welcome page opens while I am starting to type in a python file
Remove start page and its build dependencies
I would have 2 questions:
I opened a python file with Xcode and it seems to have recognized it, but still the syntax coloring doesn't seem like python, so how to make it correspond ?
I tried to create a new Python project with Xcode following the instructions on this website: https://ericasadun.com/2016/12/04/running-python-in-xcode-step-by-step/ , but gives me "ERROR: unknown command "$(SRCROOT)/Work.py". I want to precise that it didn't give me the executable directly I had to select it myself in "/Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9". I tried some suggested fixes as changing the "$(SRCROOT)/Work.py" by the path till Work.py : "/Users/user/Downloads/Python/Work.py". But still it persisted on the same error. So the question is how to make Xcode run a python code given the issues I encountered ?
Thanks !
So I fixed it by searching Work.py file and where I saved it. After that hit right click on the Work.py file and click on Get Info. Expand General: and then copy the text from Where:. Paste that in that Argument box and close the windows. Everything should work now.
During a presentation yesterday I had a colleague run one of my scripts on a fresh installation of Python 3.8.1. It was able to create and write to a csv file in his folder (proof that the csv library was working correctly), but everything else failed due to not being able to find the needed files. To try and isolate the problem and figure out why, we tried the below simple script, which also failed.
He had this test.py script in "D:/TEST", which also contained some folders and image files. Running this script printed nothing to the console. No empty list, no error message, no newline. Maybe the print() function was also not working, but I didn't get around to testing that.
import os
print(os.listdir())
This script works fine on my computer and my other colleagues computers (all Windows 10, similar hardware). I didn't have time to look into the issue more thoroughly and don't have access to his computer anymore. What could be the problem? What other things could I have him look into in order to fix this? In case this problem appears again during a future presentation, what steps could I take to figure out the cause of it?
My colleague uninstalled Python and reinstalled it. After doing this apparently the "python" command will no longer run his scripts, but using "py" instead will. Now that he is using "py" to run his scripts, it is working as expected.
I have just written a bunch of lines of code on the Python prompt at the terminal. Now, I want to save all those lines of code to a .py file.
I am unable to find out how to do that. The only thing that I could find on StackOverflow was this answer but it shows only how to do it in an iPython notebook. I am not using an iPython notebook. I am running the code at the command line on the terminal.
I tried to follow that answer (because just in case) and ran the %save magic command on the terminal but it gave a SyntaxError.
So, how do save it?
Thanks!
See http://blog.e-shell.org/174 . As wu explains, the python prompt is using readline, and you can import a Python library to access this.
>>> import readline
>>> readline.write_history_file('/path/to/history.txt')
You can trying using another interpreter : bpython , I belive it has what you need,check it out.
Save the code you've entered to a file.
You seem to be affected by the misconception, that the python environment is workspace-centered (similar to what I know from Smalltalk and some LISP variants):
fire up with an initial workspace
modify by your liking
store the result
This is unfortunately not the case. While you can import existing files, the other option is to specify an existing file as initially to be loaded and keep the interpreter open by using the -i option.
It really depends on your terminal for the exact commands.
The general idea is to copy everything (if possible) or one page at a time from the terminal into a text editor and then clean the >>> prompts (and possibly other formatting problems) in the text editor.
But anyway, typing a lot of commands directly in the execution environment if really bad practice. At least you test a handful of lines and immediately save them in a file. IDLE is great at this game...