Pycharm error in one project and not the other - python-3.x

I am running PyCharm Community Edition 2021.1.3 and having this strange problem where I would run a file in one project and it would produce an error but run the same file in another project and it would produce no error. I have tried figuring out what is causing the problem but to no avail. I have discovered that any file with an import, in the first project (The one that gives me an error) would always get the error but a file without any imports works fine. This is the same whether the code after is correct or not. If someone could please help me with this problem as I could have missed something simple it would be much appreciated.
Code used in both projects:
from flask import Flask, render_template
from forms import RecommendationForm
app = Flask(__name__)
app.config['SECRET_KEY'] = '777'
#app.route('/')
def home():
form = RecommendationForm()
return render_template('index.html', form=form)
if __name__ == '__main__':
app.run()
Screenshot of project producing error:
Pycharm project with error
Screenshot of project working correctly:
Diffrent Pycharm project with the same code
The error tends to generally look the same with a few variations. Here is an example running different code but in the first project.
Diffrent code run in the error producing project
The rest of the error logs from the above example

Related

Relative path code is giving me a different error for auto-py-to-exe

I am trying to convert my .py to .exe using auto-py-to-exe. I have two logos that I want on my file, and the first edition of the app worked with no issues, and the onefile option worked great. However, I have made updates to the app over the past month, and when I go to use onefile again, the program couldn't locate my logos. So, I pasted the relative path code below. Now I get an error which I have screenshotted. I have not the slightest clue how to fix this. If someone could give me some direction, that would be fantastic. Thank you!
IMAGE:
https://www.dropbox.com/s/zby2e8s45b9ovu6/Stack.PNG?dl=0
""" Get absolute path to resource, works for dev and for PyInstaller """
try:
# PyInstaller creates a temp and stores path in MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")```

Creating new files on heroku while app is working?

I have a python telegram bot and I have deployed it on Heroku. But problem is that my program actually creates pickled files while working. I have one database which is saving the required data and pickles to save some nested classes which I have to use later at some stage. So these pickle files are one of the important parts of the program. I am using the dill module for pickling.
I was able to save these files locally, but can't do when I am doing it in Heroku. I'll share the logs below. It is not even reaching the pickling part, but giving an error in opening the file itself.
import dill
def saving_test(test_path, test_obj):
try:
save_test_logger.info('Saving test...')
try:
save_test_logger.info('opening file')
test_file = open(test_path, 'wb')
except Exception as exc:
save_test_logger.exception('Error opening file')
return 0
dill.dump(test_obj, test_file)
save_test_logger.debug(f'file saved in {test_path}')
test_file.close()
return 1
except Exception as exc:
save_test_logger.exception('saving error')
test_file.close()
return exc
saving 859ab1303bcd4a65805e364a989ac8ca
2020-10-07T20:53:18.064670+00:00 app[web.1]: Could not open file ./test_objs/859ab1303bcd4a65805e364a989ac8ca.pkl
And I have added logging also to my program, but now I am confused about where can I see the original logs which are supposed to catch the exceptions also.
This is my first time using Heroku and I am comparatively new to programming also. So please help me out here to identify the route cause of the problem.
I found the problem. Even though I have pushed everything, directory test_objs wasn't there on Heroku. I just added 2 more lines of code to make a directory using os module if the directory does not exist. That solved the problem. I am not deleting the question so that just in case someone gets stuck or confused in a similar kind of situation, this question might be able to help them.

Getting a TabError

I am absolutely new to Django and Python and is following a tutorial someone did a few years ago to the letter. I have the following code snippet and when I save it my development Django server give me a error message:
File: "/path/to/my/file/apps.py", line 7
def ready(self):
TabError: inconsistent use of tabs and spaces in indentation
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals
When I save the file I get the error message above. I also have another file very similar to this one and that one works well. Any idea where I need to change? This code is part of code to create a profile for a user on the site when the user registers on the site.
def needs to be under name. You had added an extra space before def.
To avoid such errors in the future, use a better Text Editor or try out online auto-formatters.
Proper code-
from django.apps import AppConfig
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals

Python pickle question. I don't know why this error occurred

Before I ask you, please understand that I am not good at English. im sorry.
import...
sharedMem_chk=mp.Value(ctypes.c_bool,False)
def all_loopStop(chk):
#print("[def]all_loopStop:::ready")
while True:
if chk.value==False:
if keyboard.is_pressed('q'):
print("::stop loop::")
chk.value=True
def __init__():
test1 = mp.Process(target=all_loopStop, args=(sharedMem_chk,))
test1.start()
if __name__ == '__main__':
__init__()
This is part of my code.
It works fine when compiling and debugging, but when I write an exe file with cx_freeze and run the exe file I get the following error message:
_pickle.PicklingError: Can't pickle : attribute lookup all_loopStop on main failed
After searching for an hour, I think the reason for the error is data not serialized, but the all_loopStop function has not needed serialized data, so I don't know why the error occurs.
The development environment is python3.7 32bit, and I'll also attach a detailed debug console window.
I need your help very much. I apologize once again for asking questions with my poor English.
Debug console screenshot

Message: Tried to run command without establishing a connection

New to this, apologies for the novice question.
Trying to run a script using Python, Selenium and the unittest module. Have the typical setUp(), test_1, test_2, tearDown() method structure. Since I've added in more than one test, I get the following error:
selenium.common.exceptions.InvalidSessionIdException: Message: Tried to run command without establishing a connection
How can I resolve this?
I have looked into similar problems people have been facing with this issue, but in almost all cases the issue is not related to anything I am encountering (cronjobs for example)
My program looks like this...
class MyTest(unittest.TestCase):
#classmethod
def setUpClass(cls):
#my setup code here...
cls.driver = webdriver.Firefox(executable_path='my_gecko_driver')
cls.driver.get('www.my_url.com')
cls.driver...... # various other tasks
def test_1(self):
# my test code here....
foo = self.driver.find_element_by_xpath('/button_elem/')
foo.click()
# etc etc....
def test_2(self):
# my test code here....
bar = self.driver.find_element_by_xpath('/button_elem/')
bar.click()
# etc etc....
#classmethod
def tearDown(cls):
print('Entered tearDown function.')
# close the browser window
cls.driver.close()
if __name__ == '__main__':
unittest.main()
Before I added the second test, the test ran successfully.
Now I am getting the error:
selenium.common.exceptions.InvalidSessionIdException: Message: Tried to run command without establishing a connection
I suspect this is to do with the tearDown method perhaps not working correctly? However I thought this method was called at the end of every test_x upon finishing.
I have also noticed that Pycharm is highlighting 'driver' in the line 'cls.driver.close()' which I am also not too sure about. It says unresolved attribute reference' however is this not created in the setUp() method?
Try switching explicitly between tabs before closing them.
main_page = driver.window_handles[0]
driver.switch_to.window(main_page)
this is because multiple browser sessions are opened at your machine.
if you are on linux run the command
killall firefox
and try to run your script again. This should fix error for you.

Resources