alfonsogonzalez / AWP

Astrodynamics with Python book, software, and videos. Spacecraft trajectory and attitude modeling and simulation
275 stars 67 forks source link

Fix Error #30

Closed gleatd01 closed 2 years ago

gleatd01 commented 2 years ago

python .\Spacecraft_hello_world.py Traceback (most recent call last): File ".\Spacecraft_hello_world.py", line 12, in from Spacecraft import Spacecraft as SC ModuleNotFoundError: No module named 'Spacecraft'

gleatd01 commented 2 years ago

Error importing Spacecraft

alfonsogonzalez commented 2 years ago

@gleatd01 I see you closed the pull request, but nevertheless this brings up a good point.

I am making a conscious effort to move away from adding to PYTHONPATH within the Python scripts themselves, and instead move towards environment variables. Here is a video where I go over PYTHONPATH: https://youtu.be/ey-JL8FCHnk

Also, it is bad practice for me to post any paths without using the os.path.join function, since forward slashes (/) don't work for Windows.

I actually have some cleaning up to do regarding this, since there are some scripts that use the forward slashes

gleatd01 commented 2 years ago

I'll look into this some (using windows interestingly enough) might see a pull request in the future with a potential fix.

alfonsogonzalez commented 2 years ago

@gleatd01 For now, I want to be the only one committing to this repository (but that may change in the future). However, definitely feel free to create Issues, which I can then close with attached pull requests. Someone just posted a really interesting Issue regarding calculating umbra/penumbra and we've been having a good conversation in the comments about how I should implement it.

The solutions I am using for this path situation is environment variables and the "os" Python module. For example, take a look at src/python_tools/spice_data.py Here, the path to the SPICE kernels is defined as relative to the script itself (not matter from where it is called from) by using the file variable, and then the relative path is created using the os.path.join function, so it is operating system independent.

However, I have some cleaning up to do, since lines 16-18 should also be separating the directory and filename, so there are no "/" characters in the source code. I'll get to that this weekend.

I have decided to move away from the "from sys import path; path.append()" solution because unless you are using absolute paths, it is dependent on where you are calling the script from. For example, saying: path.append( '../src/python_tools' ) would require you to run that script from 1 directory above the src directory, which is not always going to be true, and is very limiting. I have used this library in different repos, so its very convenient to not have to run scripts from a specific location. Instead, by adding the src/python_tools directory to PYTHONPATH, you can import this library from anywhere on your computer by just saying from Spacecraft import Spacecraft as SC

Also, I believe its good practice for anyone to get familiar with how to use the command line and environment variables, which is why I made that PYTHONPATH video so anyone who is not familiar with it currently can have a guide on how to use this repo.