简体   繁体   中英

Execute Jupyter Notebook / Colab instruction in PyCharm

I cannot integrate this code 1 to 1 in PyCharm because the instructions such as !mkdir ~/.kaggle are not recognized. How can I rebuild this source code so that it also works in Pycharm? Thanks in advance.

import os
import json
import zipfile
import glob

username_kaggle = "dummy"
token_kaggle = "token"
savepath = "/content/dataset/retailrocket"

if not os.path.exists(savepath):
    os.makedirs(savepath)
os.chdir(savepath)

# Download the dataset from kaggle
!pip install kaggle

!mkdir ~/.kaggle
!touch ~/.kaggle/kaggle.json

api_token = {"username":username_kaggle,"key":token_kaggle}
with open('/root/.kaggle/kaggle.json', 'w') as file:
    json.dump(api_token, file)
!kaggle datasets download -d retailrocket/ecommerce-dataset

I want to write a real answer instead of a comment here.

!pip install kaggle
!mkdir ~/.kaggle
!touch ~/.kaggle/kaggle.json
!kaggle datasets download -d retailrocket/ecommerce-datase

These parts of the code won't work because the exclamation mark commans isnot normally python code. These only work in Ipython. Anything with the exclamation mark is supposed to run in the system console instead of in python itself.

As you noted in your comments you are using windows. So most of these commands wont work.

pip install kaggle

This command will work normally in in the windows shell as long as pip is installed.

!mkdir ~/.kaggle

This command creates a new folder and should look like the following in windows (It depends on where you want to save that json file):

mkdir "C:\kaggle"

Modifiy this path to where you need the file.

touch

Touch creates and empty file in a specific folder.
On windows this command can be run like this:

type nul > "C:\kaggle\kaggle.json"

Here you again need to use the path you used with the mkdir command up top.

I dont know what kaggle datasets download -d retailrocket/ecommerce-dataset exactly does but it seems its dowloading some dataset from kaggle.
I imagine you can run this command once in your console and forget about afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM