2

I am using Python 3.7 on windows. I installed Pycharm and successfully wrote a script which I am now trying to schedule using Windows Task Scheduler, which comes with its on set of complications. One thing I have noticed about Pycharm is I think it has created a separate file directory to store any packages I add to a script (maybe in something called "venv"? Instead of using the User/Python37/Scripts file.

This means when I try to run my script in the command prompt, python.exe looks for packages and cannot find them. Also if I go into my Pycharm project folder is see another instance of a Python Application file different than the Python Application stored in User/Python37. I think this also creates problems but I am not 100% sure.

I am hoping someone has seen this issue and can help me align where Pycharm stores packages. Any help would be greatly appreciated.

Steve Burt
  • 69
  • 1
  • 8
  • Yes, by default programs like anaconda or pycharm stores their installed packages in different locations than your python installation. You can see this by printing sys.path both from terminal and from pycharm. – unlut Oct 30 '19 at 15:05

2 Answers2

0

You can also simply add your script/package into your python path.

For that follow this awser : How to add to the PYTHONPATH in Windows, so it finds my modules/packages?

Florian Bernard
  • 2,561
  • 1
  • 9
  • 22
  • So add each file path containing Python packages to PYTHONPATH? – Steve Burt Oct 30 '19 at 14:59
  • Depending on the structure, but if its like this `/path/to/my/scripts/` you add this path to the pythonpath and then you can call all scripts inside it. – Florian Bernard Oct 30 '19 at 15:03
  • 1
    It is dangerous to add the virtual environment's python's path to the windows PATH: it may generate conflicts on which library/python version should be used. Even if you manage to keep everything working, you're just invalidating the whole concept of the virtual environment – Gsk Oct 30 '19 at 15:21
  • This answer seemed on the right track but GSK's reply gave me some pause. – Steve Burt Oct 30 '19 at 17:15
  • @SteveBurt It's dependent on your scripts, if they rely on several packages, you may be tempting to keep them in there environement. Howerver, if they have no dependencies, go ahead with my awnser at it will be easier for you and it will right to do so. – Florian Bernard Oct 30 '19 at 17:32
0

PyCharm creates a virtual environment (venv) where you can keep the python version and the libraries used in a specific project.
You can add libraries to the specific environment through the Pycharm GUI:

File > Settings > Project: Patterns > Project Interpreter > Install (green +)

Find your package and click Install Package in your venv.
You can see all the installed packages and their version in the path:

File > Settings > Project: Patterns > Project Interpreter

You can also use pip install, if you want to go through CLI, but be sure to use the virtual environment's pip (located in project_folder/venv/Scripts).

If for some reason you want to use the python version outside the virtual environment, go to the following path in PyCharm:

File > Settings > Project: Patterns > Project Interpreter

In the Project interpeter dropdown menu, you should find other python's location; choose the one you prefear. If you don't see your standard python version (usually in C:\python\python.exe, or something similar), you can add it by clicking on the settings menu, and specify the path to the desired python version in Base interpreter:.
In this window, you can find other settings to configure the interpreter as you want.

Gsk
  • 2,929
  • 5
  • 22
  • 29
  • If my understanding is correct, this answer helps how to manipulate interpreters within PyCharm. But my challenge is once I go to run the Python script in something like the command line or by just launching Python 3.7 I am getting errors. Is there a way to quickly copy over the packages in venv to Python 3.7's script folder? – Steve Burt Oct 30 '19 at 17:13
  • 1
    If you want to run from the cmd line your python script in your venv, I suggest you to use the venv's python: `C:\path\to\your\venvs\python.exe your_file.py`. If you're committed copying all the libraries from the venv to the general python, go to the pip in your venv (probably `project/venv/Scripts`) in cmd line, run `pip freeze > requirements.txt` to save all the used libraries. Go one folder back, and run `pip install Scripts/requirements.txt` to install the venv's libraries into the general python installation – Gsk Oct 31 '19 at 10:25