zacharybmeyers / janggi-game

This project creates the logic for a playable game of Janggi (Korean chess) with a GUI built on Pygame.
2 stars 0 forks source link

Running individual modules with PyCharm #23

Closed zacharybmeyers closed 3 years ago

zacharybmeyers commented 3 years ago

Extra Note: I had to update to Python 3.9 for the new type hinting to work successfully. Would this be a new requirement?

The shell scripts all work!

In addition, I now know that I can successfully run individual files like game.py from the parent directory in the terminal (janggi-game) by using something like python janggi/game.py if I want to.

However, if I want to run game.py in PyCharm (so that I can use the debugger), I get a FileNotFoundError when piece.py tries to access janggi-game/assets to load images for each piece, since it is one directory higher in the project structure.

I tried changing the path in piece.py from os.join.path('assets', filename) to os.join.path('..', 'assets', filename) which worked! But this broke the shell scripts...

Can you help me understand what's going on and how to fix it?

asiderop commented 3 years ago

Let's assume $ROOT points to wherever your Git workspace is checked out. This means the janggi modules are available at $ROOT/janggi/... and all the other asset files are also starting from that path (e.g. $ROOT/assets/...).

So whenever you execute any of the code, you always need $ROOT to be the "working directory". That is, no matter what Python module you're trying to run, you need to make Python think you're doing it from $ROOT. In PyCharm for each run/debug configuration there is a text field called "Working directory"; this should always be set to $ROOT.

When done this way, the Python code (e.g. open("path/to/file"), os.path.join("path/to/dir", filename)) will use $ROOT as the relative start for any path. (Of course if the path starts with a /, then it's not relative to anything---it's absolute.)

asiderop commented 3 years ago

Also, you shouldn't need Python 3.9 for the annotations to work... Python 3.6+ should be fine, though it was refined in 3.7. What version were you on before?

zacharybmeyers commented 3 years ago

Also, you shouldn't need Python 3.9 for the annotations to work... Python 3.6+ should be fine, though it was refined in 3.7. What version were you on before?

I was on 3.8, but it wasn't all the annotations, only the list annotations that specified what values were held inside. Related to https://www.python.org/dev/peps/pep-0585/ I think...

zacharybmeyers commented 3 years ago

Let's assume $ROOT points to wherever your Git workspace is checked out. This means the janggi modules are available at $ROOT/janggi/... and all the other asset files are also starting from that path (e.g. $ROOT/assets/...).

So whenever you execute any of the code, you always need $ROOT to be the "working directory". That is, no matter what Python module you're trying to run, you need to make Python think you're doing it from $ROOT. In PyCharm for each run/debug configuration there is a text field called "Working directory"; this should always be set to $ROOT.

When done this way, the Python code (e.g. open("path/to/file"), os.path.join("path/to/dir", filename)) will use $ROOT as the relative start for any path. (Of course if the path starts with a /, then it's not relative to anything---it's absolute.)

Gotcha. So I'll try making sure the configuration has the working directory specified (I think we already talked about this... but thank you)

zacharybmeyers commented 3 years ago

Got it working, thanks again!