Issue
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:
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.
Solution
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
See the cog on the right? Click it, "Show all". This will show up, listing all venvs that PyCharm can use for your project:
With your venv selected, click the last icon at the bottom. The icon looks kinda like a folder structure.
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.
Answered By - h4z3
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.