Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import Statements?
#1
Hi, I'm a brand new user of Python and I'm having some trouble with import statements.

I have the following code which works perfectly fine inside the IDE (PyCharm) and also outside of it when I use the .py file directly from the directory.
-------------------------
import time

condition = 1

while condition < 50000:
    print(condition)
    condition += 1
    time.sleep(1)
-------------------------
However, when I add another import statement.. It stops working?
(I also went into settings and installed the package before I did that)
-------------------------
import pyautogui
import time

condition = 1

while condition < 50000:
    print(condition)
    condition += 1
    time.sleep(1)
-------------------------
The code actually runs perfectly fine inside the IDE but it doesn't outside of it.
Outside the IDE, the console opens momentarily and then disappears as if the loop didn't even exist.

With this I suspect that it might be something wrong with the import statement..

Because it's not part of the standard Python library I'd need to do more than just that statement or something?

If anyone could help me, that'd be great!
Reply
#2
Do you get any output from print(condition)?

We have no idea what import the following means

Quote:However, when I add another import statement.. It stops working?
If there was a problem with the import you would get an error message
import xyz
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named xyz 
Reply
#3
woooee Wrote:If there was a problem with the import you would get an error message
Another possibility is that pythonpotato clicks on the file in the file manager and that this launches a console which disappears as soon as the script crashes.

@pythonpotato
Start a terminal or console separately, then type python /path/to/the/script.py in this console. You should see the error message, which is probably an ImportError as woooee said.
Reply
#4
(Dec-04-2018, 05:54 AM)Gribouillis Wrote:
woooee Wrote:If there was a problem with the import you would get an error message
Another possibility is that pythonpotato clicks on the file in the file manager and that this launches a console which disappears as soon as the script crashes.

@pythonpotato
Start a terminal or console separately, then type python /path/to/the/script.py in this console. You should see the error message, which is probably an ImportError as woooee said.

Hello, thank you for your reply, here is the following error code when I followed your instructions:
Traceback (most recent call last):
  File "E:\Python WorkSpace\venv\looping\looping.py", line 1, in <module>
    import pyautogui
ModuleNotFoundError: No module named 'pyautogui'
I already had pyautogui installed.. Which is why it worked inside the IDE just fine..

I'm at a loss with what to do now. Any help is appreciated!
Reply
#5
It seems that your program is in virtual environment. This environment probably needs to be activated when you run the script, or at least you need to use the virtual environment's python interpreter for which you installed the pyautogui module (normally in the virtual environment's 'bin' folder). I'm usually not a Windows user, so I can't tell you more about this, but other members of the forum can help you.
Reply
#6
(Dec-04-2018, 08:01 AM)Gribouillis Wrote: (normally in the virtual environment's 'bin' folder
On Windows it's Scripts folder, not bin.

(Dec-04-2018, 07:00 AM)pythonpotato Wrote: I already had pyautogui installed.. Which is why it worked inside the IDE just fine..
Do you have it installed in the venv virtual environment? When run in PyCharm, what interpreter do you use - i.e. do you use the interpreter in venv or the default system-wide one?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(Dec-04-2018, 08:20 AM)buran Wrote:
(Dec-04-2018, 08:01 AM)Gribouillis Wrote: (normally in the virtual environment's 'bin' folder
On Windows it's Scripts folder, not bin.

(Dec-04-2018, 07:00 AM)pythonpotato Wrote: I already had pyautogui installed.. Which is why it worked inside the IDE just fine..
Do you have it installed in the venv virtual environment? When run in PyCharm, what interpreter do you use - i.e. do you use the interpreter in venv or the default system-wide one?

Hello and thank you for your reply. I believe it is the interpreter in venv according to this:

[Image: zvecxUb]

Do I need to change it to the default system wide one then?
Reply
#8
Based on the image, your project uses the interpreter in the virtual environment. No need to change that, but make sure the virtual environment is activated when you run the script from command line.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
I think you can write a .bat file that activates the virtualenv then calls the interpreter on your script. Then you can simply click on the .bat file.
Reply
#10
(Dec-05-2018, 09:59 AM)Gribouillis Wrote: I think you can write a .bat file that activates the virtualenv then calls the interpreter on your script. Then you can simply click on the .bat file.

Hey thanks for your reply! I looked around and managed to get the virtual environment working and ran my script flawlessly.

However, I would like to automate the activation of the virtualenv without the batch file because I want to try to use py2exe to have my script as an executable. Would this be realistically possible at all? How would I go about this to start?

Thanks again!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020