0

I am beginner in python. I created project with three .py files and one database file in PyCharm. But don`t understand how run my project via cmd console or something else. Do I need to create start class like "main" or what to do? How can I sent my project to another people that they may run it?

On the screen I tried run .py file via cmd Windows console screenshot of python exception

7
  • How do you run it yourself? Commented Apr 9, 2017 at 18:51
  • only via PyCharm Commented Apr 9, 2017 at 18:52
  • python script.py where script is the name of the file... You ran the code fine, though. The database file can't be read. Note: python doesn't really use src folder like in Java Commented Apr 9, 2017 at 19:00
  • There are multiple things to consider. Make sure that you are launching the same .py script as is in your PyCharm's configuration. Also ensure that you are using the same python installation as does PyCharm when you run through terminal and that. Finally PyCharm might be calling your file from within ContactBook directory rather than src. Which current working directory you are using is important, i.e. you might not be able to open files because your program is looking at them in the wrong folder because working directory doesn't match. Commented Apr 9, 2017 at 19:02
  • 1
    The error you're getting is because you're already in src when trying to open src\Contacts.db, i.e. it's trying to access C:\Users\homic\PycharmProjects\ContactBook\src\src\Contacts.db. Commented Apr 9, 2017 at 19:04

3 Answers 3

2

Python program does not require "main function" as entry point, each line of a .py file will be executed sequentially. All you need is to type: python yourprogram.py in your console.

But on the other hand, many people choose to have a "main function" in their python program and only invoke this function when python program is run as a stand-alone script as opposed to being loaded as a module.

def main():
  # do something

if __name__ == "__main__": 
  main()
Sign up to request clarification or add additional context in comments.

Comments

0

Your code ran.

The error seems to suggest that it's running from the src folder and trying to open a src subdirectory with a database.

Without giving absolute paths, all file references are relative to the executed script. So, just connect to the database name, not src/Contacts.db

Comments

0

Each of your .py file is an executable. Depending on how you have coded, these files might be inter-dependent. In that case, there might be a main file(with any name, need not name it main.py, could be hello.py). So, you can run that file with python <filename>. As far as the language is concerned, there is no need to create a special class like main. Though you are free to do that.

General style it to include a statement in the end of the main file like suppose you have three files hello.py, bye.py, work.py, where

from work import *
from bye import *

def hello():
    print "Hello"

if __name__ == "__main__":
    hello()

You can run python hello.py

For sharing, you can create a zip of the folder and share that zip or you can create a git hub repository and share the github link.

The error is in opening the file. please check for spelling errors and the location of the db file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.