Python Forum
Import Statements? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Import Statements? (/thread-14507.html)

Pages: 1 2


Import Statements? - pythonpotato - Dec-04-2018

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!


RE: Import Statements? - woooee - Dec-04-2018

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 



RE: Import Statements? - Gribouillis - Dec-04-2018

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.


RE: Import Statements? - pythonpotato - Dec-04-2018

(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!


RE: Import Statements? - Gribouillis - Dec-04-2018

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.


RE: Import Statements? - buran - Dec-04-2018

(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?


RE: Import Statements? - pythonpotato - Dec-05-2018

(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?


RE: Import Statements? - buran - Dec-05-2018

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.


RE: Import Statements? - Gribouillis - Dec-05-2018

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.


RE: Import Statements? - pythonpotato - Dec-07-2018

(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!