1

There are loads of answers on how to import modules from other folders.

The answer always seems to be along the lines of:

import sys
sys.path.insert(0,"c://UserName//MyFolder//MyBeautifulCode")
import myscript as ms

after which you can run ms.my_fun(x,y,z) where my_fun() is defined in c://UserName//MyFolder//MyBeautifulCode//myscript.py

The code runs; however, what doesn't work is that, this way, I do not get the usual tooltip showing the arguments of my_fun(); if instead I copy myscript.py in the very same folder as the script I am currently running, then, yes, I do get the tooltip. What I mean is I don't see something like this: enter image description here

I have tried with both PyCharm and Spyder and the behaviour, in this respect, is the same for both.

I suppose this happens because c://UserName//MyFolder//MyBeautifulCode//myscript.py gets added to the path only when the script is run so, before it is run, the IDE doesn't find my_fun()

Is this correct? If so, is the only solution to manually add c://UserName//MyFolder//MyBeautifulCode//myscript.py to the path ?

By the way, I am talking about a couple of functions which I reuse in 3 separate programs I am running. It is nothing worth publishing on github or pip as a package or anything like that.

Pythonista anonymous
  • 8,140
  • 20
  • 70
  • 112

2 Answers2

2

For PyCharm, you need to set up your project's venv settings to include that path as well. It took me a lot of time to find it at first - and I used google to search for this! but apparently PyCharm hid the option deeper... well, see for yourself.

Go to the Settings, Project: [your project name here], Python Interpreter

image contains PyCharm's Project Interpreter settings

See the cog on the right? Click it, "Show all". This will show up, listing all venvs that PyCharm can use for your project:

image contains list of Python Interpreters recognised by PyCharm

With your venv selected, click the last icon at the bottom. The icon looks kinda like a folder structure.

image contains list of Interpreter Paths recognised by selected interpreter/venv

Now we see all the paths recognised by the selected interpreter in PyCharm. We can click + to add a new path. Manually added path will have "(added by user)` at the end, just like in the pic.

h4z3
  • 5,265
  • 1
  • 15
  • 29
  • Thanks! Do I need to do this for every project or is there a way to tell PyCharm to add that folder to the path of every project? btw, this confirms my impression that PyCharm is very powerful but also very very very hard to use! – Pythonista anonymous Feb 20 '21 at 16:52
  • 1
    @Pythonistaanonymous In my case, I use the same venv for multiple projects (as they are deployed in the same system and I cannot install new packages there - the venv stays the same no matter what). When choosing venv for your project, you have to select the option to have the venv available for other projects - and then in other ones select it instead of creating new venvs each time. – h4z3 Feb 20 '21 at 22:10
1

If you insert a path into the Python path in your code, it's only interpreted at runtime. To get your IDE to know about your library, you will have to add it to the Python Path, e.g. like described in this question: https://stackoverflow.com/a/55209725/5660315.

VirtualScooter
  • 1,792
  • 3
  • 18
  • 28
  • Is this solution IDE-specific I.e. will each IDE have a similar setting? Or, if I add the fodler to the Python path, then every IDE will find it? – Pythonista anonymous Feb 20 '21 at 16:50
  • Each IDE will have a similar setting, since the code that generates the tooltip is (compiled) Python code that also needs a Python path. – VirtualScooter Feb 20 '21 at 16:53