I have three .py files in my projects. One is a file2.py which imports googlesearch, selenium etc. Other is file3.py which imports random. And the last one is my main.py which imports file2.py and file3.py.
In PyCharm my program works just fine, with every module installed correctly, but when I try to run my main.py from the terminal it gives me and error that module 'googlesearch' is not found. Probably the module itself doesn't matter, it just happens to be the first used in my program and so the error.
I found this issue when trying to save my project to .exe and the program would just appear and disappear rapidly. I managed to slow it down and it gives me the same error.
My project file looks something like this:
.idea - folder
__pychace__ - folder
venv - folder
.google-cookie
file2.py
main.py
In my venv folder I have:
__pycache__ - folder
Include - folder
Lib (which has all my packages and modules) - folder
Scripts - folder
file3.py
pyvenv.cfg
Now I don't really understand why file3.py is in my venv. And I don't know how can I make my exe file detect my imported modules. I presume somehow that my lib is not detected. I am quite a beginner and if you guys have any ideas I would really appreciate it.
I understand your problem completely as I once had PyCharm as the IDE for Python.
So here is how PyCharm works:
When you create a new project in PyCharm, it automatically creates a virtual environment for the project.
A folder named venv
is created (as you mentioned).
In this folder a sub folder namely libs
can be seen. This is the folder in which all those modules are installed for the currently active project. This means that the modules you install are not getting installed in the Python's PATH
but being downloaded in the venv
folder's library which is exclusive for the project.
Now I hope this explanation is clear.
What is the solution?
It is very simple. When you install Python, it is directly added to PATH
which means you can easily access it using Command Prompt
. To access the modules without using PyCharm and using IDLE or whatever else, you need install modules in the Python's make directory that is downloading using CMD
to the PATH
from where Python can take out any module without installing again and again or using PyCharm.
Steps:
Check if Python is added to PATH
. It is simple. Open CMD
and type python
and press enter. You would see the Python's Interactive mode in CMD
. If you do not see that and instead see this error:
'python' is not recognized as an internal or external command,
operable program or batch file.
I recommend you to add Python
to PATH
with reference to the site: https://www.educative.io/edpresso/how-to-add-python-to-path-variable-in-windows
The second step is to execute a pip installation command. (Would only work with Python added to PATH. Check point 1 for reference.) The command:
py -m pip install <module name>
Add the name of the package you want to install and the module would be installed.
Now you would be able to use that particular module any where you want without using PyCharm.
Personal Advice: You VS Code instead of PyCharm as it is lighter and faster than PyCharm and doesn't really bother you with such errors. Also in VS Code you get a choice to activate a Virtual Environment. You can use it or not depending on what you are doing.