Importing modules from other folders: how to display function arguments in Spyder or PyCharm?

Viewed 145

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.

2 Answers

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.

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.

Related